﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using EFong.Data;
using EFong.BL.Const;

namespace EFong.BL
{
    public class BusinessBL
    {
        #region Static Data

        private static object _syncObj = new object();

        private EFongEntities entities = EFongEntitiesManager.GetInstance();

        #endregion

        #region Method

        #region New Update

        public string NewFlightOrder(Contract.FlightOrder flightOrder)
        {
            checkFlightOrder(flightOrder);
            DateTime dateTime = DateTime.Now;

            FinanceBL financeBL = new FinanceBL();

            FlightOrder newFlightOrder = new FlightOrder();
            newFlightOrder.ID = flightOrder.ID;
            newFlightOrder.Ref = new BaseBL().GetNewNr("Flight");
            newFlightOrder.Address = flightOrder.Address;
            newFlightOrder.ContactPerson = flightOrder.ContactPerson;
            newFlightOrder.ContactPersonTel = flightOrder.ContactPersonTel;
            newFlightOrder.CreditTerm = flightOrder.CreditTerm;
            newFlightOrder.CurrencyID = flightOrder.Currency.ID;
            newFlightOrder.CustomerID = flightOrder.Customer.ID;
            //newFlightOrder.ExchageRate = flightOrder.ExchangeRate;
            newFlightOrder.TotalIntergral = flightOrder.TotalIntergral.Value;
            newFlightOrder.IsInternational = flightOrder.IsInternational;
            newFlightOrder.OrderDate = flightOrder.OrderDate.Value;
            newFlightOrder.OrderTicketUserID = flightOrder.OrderTicketUser.ID;
            newFlightOrder.OrderUserID = flightOrder.OrderUser.ID;
            //newFlightOrder.Phone = flightOrder.Phone;
            newFlightOrder.PrintTotalPrice = flightOrder.PrintTotalPrice;
            newFlightOrder.ProvideTicketDate = flightOrder.ProvideTicketDate;
            newFlightOrder.ProvideTicketUserID = flightOrder.ProvideTicketUser.ID;
            newFlightOrder.DeliveryDate = flightOrder.DeliveryDate;
            newFlightOrder.Remark = flightOrder.Remark;
            newFlightOrder.SalesmanID = flightOrder.Salesman.ID;
            newFlightOrder.Status = flightOrder.Status;
            newFlightOrder.TotalAccountPayable = flightOrder.TotalAccountPayable.Value;
            newFlightOrder.TotalAccountReceivable = flightOrder.TotalAccountReceivable;
            newFlightOrder.TotalIntergral = flightOrder.TotalIntergral.Value;
            newFlightOrder.TotalProfit = flightOrder.TotalProfit.Value;
            //newFlightOrder.VendorID = flightOrder.Vendor.ID;
            newFlightOrder.VendorCurrencyID = flightOrder.VendorCurrency.ID;
            newFlightOrder.VoidCause = null;
            newFlightOrder.CustomerStatus = flightOrder.CustomerStatus;
            //newFlightOrder.VendorStatus = "已付款";
            newFlightOrder.CreateTime = dateTime;
            newFlightOrder.UpdateTime = dateTime;

            entities.AddToFlightOrder(newFlightOrder);

            List<BankAccount> bankAccountList = (from b in entities.BankAccount
                                                 select b).ToList();

            for(int j = 0;j < flightOrder.Details.Count;j++)
            {
                Contract.FlightOrderDatail flightOrderDatail = flightOrder.Details[j];
                FlightOrderDetail newFlightOrderDetail = new FlightOrderDetail();
                newFlightOrderDetail.Seq = j;
                newFlightOrderDetail.ID = flightOrderDatail.ID;
                newFlightOrderDetail.AccountPayable = flightOrderDatail.AccountPayable.Value;
                newFlightOrderDetail.AccountReceivable = flightOrderDatail.AccountReceivable;
                newFlightOrderDetail.AirwaysOrderNo = flightOrderDatail.AirwaysOrderNo;
                newFlightOrderDetail.Cost = flightOrderDatail.Cost;
                newFlightOrderDetail.FlightOrderID = newFlightOrder.ID;
                newFlightOrderDetail.NetPrice = flightOrderDatail.NetPrice;
                newFlightOrderDetail.PassengerID = flightOrderDatail.Passenger.ID;
                newFlightOrderDetail.Price = flightOrderDatail.Price;
                newFlightOrderDetail.Profit = flightOrderDatail.Profit.Value;
                newFlightOrderDetail.Remark = flightOrderDatail.Remark;
                newFlightOrderDetail.Status = flightOrderDatail.Status;
                newFlightOrderDetail.Intergral = flightOrderDatail.Intergral;
                newFlightOrderDetail.Tax1 = flightOrderDatail.Tax1;
                newFlightOrderDetail.Tax2 = flightOrderDatail.Tax2;
                newFlightOrderDetail.TicketNo = flightOrderDatail.TicketNo;
                newFlightOrderDetail.VendorDiscount = flightOrderDatail.VendorDiscount;
                newFlightOrderDetail.WayType = flightOrderDatail.WayType;
                newFlightOrderDetail.VendorID = flightOrderDatail.Vendor.ID;
                newFlightOrderDetail.VendorCurrencyID = flightOrderDatail.VendorCurrency.ID;
                newFlightOrderDetail.ExchangeRate = flightOrderDatail.ExchangeRate;
                newFlightOrderDetail.Discount = flightOrderDatail.Discount;
                newFlightOrderDetail.VendorStatus = "已付款";
                newFlightOrderDetail.CreateTime = dateTime;
                newFlightOrderDetail.UpdateTime = dateTime;

                entities.AddToFlightOrderDetail(newFlightOrderDetail);

                if (flightOrderDatail.PaymentType != "现金")
                {
                    BankAccount bankAccount = (from b in bankAccountList
                                               where b.ID == flightOrderDatail.BankAccount.ID
                                               select b).SingleOrDefault();
                    decimal 余额 = bankAccount.Amount - flightOrderDatail.AccountPayable.Value;
                    if (余额 < 0 && !bankAccount.CanNegative)
                        throw new BusinessException(BusinessFail.余额不足);

                    bankAccount.Amount = 余额;
                }
                else 
                {
                    decimal cash = financeBL.GetCash();
                    financeBL.SetCash(cash - flightOrderDatail.AccountPayable.Value, false);
                }

                VendorSettlement vendorSettlement = new VendorSettlement();
                vendorSettlement.ID = Guid.NewGuid();
                vendorSettlement.PayType = flightOrderDatail.PaymentType;
                vendorSettlement.PostingDate = flightOrder.OrderDate.Value;
                vendorSettlement.Ref = newFlightOrderDetail.TicketNo;
                vendorSettlement.Remark = string.Empty;
                vendorSettlement.Amount = flightOrderDatail.AccountPayable.Value;
                vendorSettlement.ExchangeRate = flightOrderDatail.ExchangeRate;
                vendorSettlement.CurrencyID = flightOrder.VendorCurrency.ID;
                if (flightOrderDatail.BankAccount != null)
                    vendorSettlement.BankAccountID = flightOrderDatail.BankAccount.ID;
                vendorSettlement.CheckNo = string.Empty;
                vendorSettlement.SourceID = flightOrderDatail.ID;
                vendorSettlement.Type = flightOrder.OrderType;
                vendorSettlement.VendorID = flightOrderDatail.Vendor.ID;

                entities.AddToVendorSettlement(vendorSettlement);

                foreach (Contract.InsurancePolicy insurancePolicy in flightOrderDatail.InsurancePolicyList)
                {
                    InsurancePolicy dataInsurancePolicy = (from ip in entities.InsurancePolicy
                                                           where ip.ID == insurancePolicy.ID
                                                           select ip).SingleOrDefault();
                    dataInsurancePolicy.Status = "已用";
                    dataInsurancePolicy.UsedDate = flightOrder.ProvideTicketDate;
                    dataInsurancePolicy.OrderDate = dateTime;
                    dataInsurancePolicy.OrderUserID = flightOrder.OrderUser.ID;

                    dataInsurancePolicy.FlightDetailID = flightOrderDatail.ID;
                }

                newFlightOrderDetail.InsuranceFee = flightOrderDatail.InsuranceFee;
                newFlightOrderDetail.IsInsurancePolicyFree = flightOrderDatail.IsInsurancePolicyFree;

                foreach (Contract.JourneyOrder journeyOrder in flightOrderDatail.JourneyOrderList)
                {
                    JourneyOrder dataJourneyOrder = (from ip in entities.JourneyOrder
                                                     where ip.ID == journeyOrder.ID
                                                     select ip).SingleOrDefault();
                    //if (journeyOrder.Status != "可用")
                    //    throw new BusinessException(BusinessFail.行程单不能使用, new Contract.FailValue("JourneyOrder", flightOrderDatail.JourneyOrder));
                    dataJourneyOrder.Status = "已用";
                    dataJourneyOrder.UsedDate = flightOrder.ProvideTicketDate;
                    dataJourneyOrder.FlightDetailID = flightOrderDatail.ID;
                }

                //foreach (Contract.FlightWay flightWay in flightOrderDatail.FlightWayList)
                for (int i = 0; i < flightOrderDatail.FlightWayList.Count; i++)
                {
                    Contract.FlightWay flightWay = flightOrderDatail.FlightWayList[i];
                    FlightWay newFlightWay = new FlightWay();
                    newFlightWay.ID = flightWay.ID;
                    newFlightWay.DepartDate = convertToDateTime(flightWay.DepartDate.Value, flightWay.出发时间);
                    newFlightWay.DepartLocation = flightWay.DepartLocation;
                    newFlightWay.Destination = flightWay.Destination;
                    newFlightWay.FlightNo = flightWay.FlightNo;
                    newFlightWay.FlightOrderDetailID = newFlightOrderDetail.ID;
                    newFlightWay.Seat = flightWay.Seat;
                    newFlightWay.Seq = i;
                    newFlightWay.CreateTime = dateTime;
                    newFlightWay.UpdateTime = dateTime;

                    entities.AddToFlightWay(newFlightWay);
                }
            }

            entities.SaveChanges();
            financeBL.Save();

            return newFlightOrder.Ref;
        }

        private void checkFlightOrder(Contract.FlightOrder flightOrder)
        {
            flightOrder.Address = flightOrder.Address.Trim();
            //flightOrder.ContactPerson = flightOrder.ContactPerson.Trim();
            //flightOrder.ContactPersonTel = flightOrder.ContactPersonTel.Trim();
            flightOrder.CreditTerm = flightOrder.CreditTerm.Trim();
            flightOrder.Remark = flightOrder.Remark.Trim();

            if (flightOrder.Address == string.Empty)
                throw new BusinessException(VerificationText.请输入地址);
            //if (flightOrder.ContactPerson == string.Empty)
            //    throw new BusinessException(VerificationText.请输入联系人);
            //if (flightOrder.ContactPersonTel == string.Empty)
            //    throw new BusinessException(VerificationText.请输入联系人电话);
            if (flightOrder.CreditTerm == string.Empty)
                throw new BusinessException(VerificationText.请输入客户结算方式);
            if (flightOrder.Customer == null)
                throw new BusinessException(VerificationText.请输入客户);
            if (flightOrder.OrderDate == null)
                throw new BusinessException(VerificationText.请输入开单日期);
            if (flightOrder.OrderTicketUser == null)
                throw new BusinessException(VerificationText.请输入订票人);
            if (flightOrder.OrderUser == null)
                throw new BusinessException(VerificationText.请输入经办人);
            if (flightOrder.ProvideTicketDate == DateTime.MinValue)
                throw new BusinessException(VerificationText.请输入订票日期);
            if (flightOrder.Details.Count == 0)
                throw new BusinessException(VerificationText.请输入飞机票订单明细);

            foreach (Contract.FlightOrderDatail flightOrderDetail in flightOrder.Details)
            {
                checkFlightOrderDetail(flightOrderDetail);
            }
        }

        private void checkFlightOrderDetail(Contract.FlightOrderDatail flightOrderDetail)
        {
            flightOrderDetail.AirwaysOrderNo = flightOrderDetail.AirwaysOrderNo.Trim();
            flightOrderDetail.Remark = flightOrderDetail.Remark.Trim();
            flightOrderDetail.TicketNo = flightOrderDetail.TicketNo.Trim();
            flightOrderDetail.WayType = flightOrderDetail.WayType.Trim();

            //if (flightOrderDetail.AirwaysOrderNo == string.Empty)
            //    throw new BusinessException(VerificationText.请输入航空公司订单号);
            if (flightOrderDetail.PaymentType == string.Empty)
                throw new BusinessException(VerificationText.请输入毅丰支付方式);
            if (flightOrderDetail.NetPrice == null || flightOrderDetail.VendorDiscount == null)
                throw new BusinessException(VerificationText.请输入净价或者返点);
            if (flightOrderDetail.Passenger == null)
                throw new BusinessException(VerificationText.请输入乘客);
            if (flightOrderDetail.TicketNo == string.Empty)
                throw new BusinessException(VerificationText.请输入机票号);
            if (flightOrderDetail.NetPrice == null && flightOrderDetail.VendorDiscount == null)
                throw new BusinessException(VerificationText.请输入净价或者返点);
            if (flightOrderDetail.Vendor == null)
                throw new BusinessException(VerificationText.请输入供应商);
            if (flightOrderDetail.VendorCurrency == null && flightOrderDetail.ExchangeRate == 0)
                throw new BusinessException(VerificationText.请输入汇率);
            if (flightOrderDetail.ExchangeRate < 0)
                throw new BusinessException(VerificationText.汇率格式不正确);

            if (flightOrderDetail.FlightWayList.Count == 0)
                throw new BusinessException(VerificationText.请输入行程明细);

            foreach (Contract.FlightWay flightWay in flightOrderDetail.FlightWayList)
                checkFlightWay(flightWay);
        }

        private void checkFlightWay(Contract.FlightWay flightWay)
        {
            flightWay.DepartLocation = flightWay.DepartLocation.Trim();
            flightWay.Destination = flightWay.Destination.Trim();
            flightWay.FlightNo = flightWay.FlightNo.Trim();
            flightWay.Seat = flightWay.Seat.Trim();

            if (flightWay.DepartLocation == string.Empty)
                throw new BusinessException(VerificationText.请输入出发地);
            if (flightWay.Destination == string.Empty)
                throw new BusinessException(VerificationText.请输入目的地);
            if (flightWay.DepartDate == null)
                throw new BusinessException(VerificationText.请输入出发日期);
            if (flightWay.FlightNo == string.Empty)
                throw new BusinessException(VerificationText.请输入航班号);
            if (flightWay.Seat == string.Empty)
                throw new BusinessException(VerificationText.请输入舱位);
            if (flightWay.DepartDate == DateTime.MinValue)
                throw new BusinessException(VerificationText.请输入出发日期);
        }

        public string UpdateFlightOrder(Contract.FlightOrder flightOrder)
        {
            DateTime dateTime = DateTime.Now;

            foreach (Contract.FlightOrderDatail flightOrderDatail in flightOrder.Details)
            {
                flightOrderDatail.FlightOrder = flightOrder;
                foreach (var fr in flightOrderDatail.FlightReturnHis)
                    fr.FlightOrderDetail = flightOrderDatail;
            }


            FlightOrder oldFlightOrder = (from fo in entities.FlightOrder
                                          where fo.ID == flightOrder.ID
                                          select fo).SingleOrDefault();
            if (oldFlightOrder == null)
            {
                return NewFlightOrder(flightOrder);
            }

            oldFlightOrder.Address = flightOrder.Address;
            oldFlightOrder.ContactPerson = flightOrder.ContactPerson;
            oldFlightOrder.ContactPersonTel = flightOrder.ContactPersonTel;
            oldFlightOrder.CreditTerm = flightOrder.CreditTerm;
            oldFlightOrder.CurrencyID = flightOrder.Currency.ID;
            oldFlightOrder.CustomerID = flightOrder.Customer.ID;
            //oldFlightOrder.ExchageRate = flightOrder.ExchangeRate;
            oldFlightOrder.TotalIntergral = flightOrder.TotalIntergral.Value;
            oldFlightOrder.IsInternational = flightOrder.IsInternational;
            oldFlightOrder.OrderDate = flightOrder.OrderDate.Value;
            oldFlightOrder.OrderTicketUserID = flightOrder.OrderTicketUser.ID;
            oldFlightOrder.OrderUserID = flightOrder.OrderUser.ID;
            //oldFlightOrder.Phone = flightOrder.Phone;
            oldFlightOrder.PrintTotalPrice = flightOrder.PrintTotalPrice;
            oldFlightOrder.ProvideTicketDate = flightOrder.ProvideTicketDate;
            oldFlightOrder.ProvideTicketUserID = flightOrder.ProvideTicketUser.ID;
            oldFlightOrder.DeliveryDate = flightOrder.DeliveryDate;
            oldFlightOrder.Remark = flightOrder.Remark;
            oldFlightOrder.SalesmanID = flightOrder.Salesman.ID;
            oldFlightOrder.Status = flightOrder.Status;
            oldFlightOrder.TotalAccountPayable = flightOrder.TotalAccountPayable.Value;
            oldFlightOrder.TotalAccountReceivable = flightOrder.TotalAccountReceivable;
            oldFlightOrder.TotalIntergral = flightOrder.TotalIntergral.Value;
            oldFlightOrder.TotalProfit = flightOrder.TotalProfit.Value;
            //oldFlightOrder.VendorID = flightOrder.Vendor.ID;
            oldFlightOrder.VendorCurrencyID = flightOrder.VendorCurrency.ID;
            oldFlightOrder.VoidCause = null;
            oldFlightOrder.UpdateTime = dateTime;

            for (int j = 0; j < flightOrder.Details.Count; j++)
            {
                Contract.FlightOrderDatail flightOrderDatail = flightOrder.Details[j];
                
                FlightOrderDetail dataFlightOrderDetail = (from fod in entities.FlightOrderDetail
                                                           where fod.ID == flightOrderDatail.ID
                                                           select fod).SingleOrDefault();
                if (dataFlightOrderDetail == null)
                {
                    dataFlightOrderDetail = new FlightOrderDetail();
                    dataFlightOrderDetail.ID = flightOrderDatail.ID;
                    entities.AddToFlightOrderDetail(dataFlightOrderDetail);
                }
                dataFlightOrderDetail.Seq = j;
                dataFlightOrderDetail.AccountPayable = flightOrderDatail.AccountPayable.Value;
                dataFlightOrderDetail.AccountReceivable = flightOrderDatail.AccountReceivable;
                dataFlightOrderDetail.AirwaysOrderNo = flightOrderDatail.AirwaysOrderNo;
                dataFlightOrderDetail.Cost = flightOrderDatail.Cost;
                dataFlightOrderDetail.FlightOrderID = flightOrder.ID;
                dataFlightOrderDetail.NetPrice = flightOrderDatail.NetPrice;
                dataFlightOrderDetail.PassengerID = flightOrderDatail.Passenger.ID;
                dataFlightOrderDetail.Price = flightOrderDatail.Price;
                dataFlightOrderDetail.Profit = flightOrderDatail.Profit.Value;
                dataFlightOrderDetail.Remark = flightOrderDatail.Remark;
                dataFlightOrderDetail.Status = flightOrderDatail.Status;
                dataFlightOrderDetail.Intergral = flightOrderDatail.Intergral;
                dataFlightOrderDetail.Tax1 = flightOrderDatail.Tax1;
                dataFlightOrderDetail.Tax2 = flightOrderDatail.Tax2;
                dataFlightOrderDetail.TicketNo = flightOrderDatail.TicketNo;
                dataFlightOrderDetail.VendorDiscount = flightOrderDatail.VendorDiscount;
                dataFlightOrderDetail.WayType = flightOrderDatail.WayType;
                dataFlightOrderDetail.VendorID = flightOrderDatail.Vendor.ID;
                dataFlightOrderDetail.VendorCurrencyID = flightOrderDatail.VendorCurrency.ID;
                dataFlightOrderDetail.ExchangeRate = flightOrderDatail.ExchangeRate;
                dataFlightOrderDetail.Discount = flightOrderDatail.Discount;
                dataFlightOrderDetail.VendorStatus = "已付款";
                dataFlightOrderDetail.UpdateTime = dateTime;

                var insurancePolicyList = from ip in entities.InsurancePolicy
                                          where ip.FlightDetailID == flightOrderDatail.ID
                                          select ip;
                foreach (InsurancePolicy dataInsurancePolicy in insurancePolicyList)
                {
                    Contract.InsurancePolicy insurancePolicy = flightOrderDatail.InsurancePolicyList.Find(ip => ip.ID == dataInsurancePolicy.ID);
                    if (insurancePolicy == null)
                    {
                        dataInsurancePolicy.Status = "可用";
                        dataInsurancePolicy.UsedDate = null;
                        dataInsurancePolicy.OrderDate = null;
                        dataInsurancePolicy.OrderUserID = null;
                        dataInsurancePolicy.FlightDetailID = null;
                    }
                }

                foreach (Contract.InsurancePolicy insurancePolicy in flightOrderDatail.InsurancePolicyList)
                {
                    InsurancePolicy dataInsurancePolicy = (from ip in entities.InsurancePolicy
                                                           where ip.ID == insurancePolicy.ID
                                                           select ip).SingleOrDefault();
                    if (dataInsurancePolicy.FlightDetailID != flightOrderDatail.ID)
                    {
                        dataInsurancePolicy.Status = "已用";
                        dataInsurancePolicy.UsedDate = flightOrder.ProvideTicketDate;
                        dataInsurancePolicy.OrderDate = dateTime;
                        dataInsurancePolicy.OrderUserID = flightOrder.OrderUser.ID;
                        dataInsurancePolicy.FlightDetailID = flightOrderDatail.ID;
                    }
                }

                dataFlightOrderDetail.InsuranceFee = flightOrderDatail.InsuranceFee;
                dataFlightOrderDetail.IsInsurancePolicyFree = flightOrderDatail.IsInsurancePolicyFree;

                var journeyOrderList = from ip in entities.JourneyOrder
                                       where ip.FlightDetailID == flightOrderDatail.ID
                                       select ip;
                foreach (JourneyOrder dataJourneyOrder in journeyOrderList)
                {
                    Contract.JourneyOrder journeyOrder = flightOrderDatail.JourneyOrderList.Find(ip => ip.ID == dataJourneyOrder.ID);
                    if (journeyOrder == null)
                    {
                        dataJourneyOrder.Status = "可用";
                        dataJourneyOrder.UsedDate = null;
                        dataJourneyOrder.FlightDetailID = null;
                    }
                }

                foreach (Contract.JourneyOrder journeyOrder in flightOrderDatail.JourneyOrderList)
                {
                    JourneyOrder dataJourneyOrder = (from ip in entities.JourneyOrder
                                                     where ip.ID == journeyOrder.ID
                                                     select ip).SingleOrDefault();

                    if(dataJourneyOrder.FlightDetailID != flightOrderDatail.ID)
                    {
                        dataJourneyOrder.Status = "已用";
                        dataJourneyOrder.UsedDate = flightOrder.ProvideTicketDate;
                        dataJourneyOrder.FlightDetailID = flightOrderDatail.ID;
                    }
                }

                flightOrderDatail.FlightWayList = flightOrderDatail.FlightWayList.OrderBy(fw => fw.DepartDate).ToList();

                //foreach (Contract.FlightWay flightWay in flightOrderDatail.FlightWayList)
                for (int i = 0; i < flightOrderDatail.FlightWayList.Count; i++)
                {
                    Contract.FlightWay flightWay = flightOrderDatail.FlightWayList[i];
                    FlightWay dataFlightWay = (from fw in entities.FlightWay
                                               where fw.ID == flightWay.ID
                                               select fw).SingleOrDefault();
                    if (dataFlightWay == null)
                    {
                        dataFlightWay = new FlightWay();
                        dataFlightWay.ID = flightWay.ID;
                        dataFlightWay.DepartDate = convertToDateTime(flightWay.DepartDate.Value, flightWay.出发时间);
                        dataFlightWay.DepartLocation = flightWay.DepartLocation;
                        dataFlightWay.Destination = flightWay.Destination;
                        dataFlightWay.FlightNo = flightWay.FlightNo;
                        dataFlightWay.FlightOrderDetailID = dataFlightOrderDetail.ID;
                        dataFlightWay.Seat = flightWay.Seat;
                        dataFlightWay.IsDelete = flightWay.IsDelete;
                        dataFlightWay.Seq = i;
                        dataFlightWay.CreateTime = dateTime;
                        dataFlightWay.UpdateTime = dateTime;

                        entities.AddToFlightWay(dataFlightWay);
                    }
                    else
                    {
                        dataFlightWay.DepartDate = convertToDateTime(flightWay.DepartDate.Value, flightWay.出发时间);
                        dataFlightWay.DepartLocation = flightWay.DepartLocation;
                        dataFlightWay.Destination = flightWay.Destination;
                        dataFlightWay.FlightNo = flightWay.FlightNo;
                        dataFlightWay.FlightOrderDetailID = dataFlightOrderDetail.ID;
                        dataFlightWay.Seat = flightWay.Seat;
                        dataFlightWay.IsDelete = flightWay.IsDelete;
                        dataFlightWay.Seq = i;
                        dataFlightWay.UpdateTime = dateTime;
                    }
                }
            }

            entities.SaveChanges();

            return oldFlightOrder.Ref;
        }

        public void VoidFlightOrder(Contract.FlightOrder flightOrder, string cause)
        {
            cause = cause.Trim();

            DateTime dateTime = DateTime.Now;


            FlightOrder oldFlightOrder = (from fo in entities.FlightOrder
                                          where fo.ID == flightOrder.ID
                                          select fo).SingleOrDefault();
            if (oldFlightOrder == null)
                throw new BusinessException(BusinessFail.飞机票订单不存在);
            if (oldFlightOrder.Status.Trim() == "已作废")
                throw new BusinessException(BusinessFail.飞机票订单已作废);
            if (oldFlightOrder.CustomerStatus.Trim() == "已付款")
                throw new BusinessException(BusinessFail.客户已付款);

            oldFlightOrder.VoidCause = cause;
            oldFlightOrder.Status = "已作废";
            oldFlightOrder.UpdateTime = dateTime;

            foreach (FlightOrderDetail flightOrderDetail in oldFlightOrder.FlightOrderDetail.ToList())
            {
                foreach (var insurancePolicy in flightOrderDetail.InsurancePolicy.ToList())
                {
                    insurancePolicy.Status = "可用";
                    insurancePolicy.UsedDate = null;
                    insurancePolicy.FlightDetailID = null;
                    insurancePolicy.OrderDate = null;
                    insurancePolicy.OrderUserID = null;
                }

                foreach (var journeyOrder in flightOrderDetail.JourneyOrder.ToList())
                {
                    journeyOrder.Status = "可用";
                    journeyOrder.UsedDate = null;
                    journeyOrder.FlightDetailID = null;
                }

                VendorSettlement vendorSettlement = (from vs in entities.VendorSettlement
                                                     where vs.SourceID == flightOrderDetail.ID
                                                     select vs).SingleOrDefault();

                if (vendorSettlement.PayType.Trim() != "现金")
                {
                    BankAccount bankAccount = (from b in entities.BankAccount
                                               where b.ID == vendorSettlement.BankAccount.ID
                                               select b).SingleOrDefault();

                    bankAccount.Amount += vendorSettlement.Amount;
                }

                entities.DeleteObject(vendorSettlement);
            }

            entities.SaveChanges();
        }

        //public void ResumeFlightOrder(Contract.FlightOrder flightOrder)
        //{
        //    DateTime dateTime = DateTime.Now;


        //    FlightOrder oldFlightOrder = (from fo in entities.FlightOrder
        //                                  where fo.ID == flightOrder.ID
        //                                  select fo).SingleOrDefault();
        //    if (oldFlightOrder == null)
        //        throw new BusinessException(BusinessFail.飞机票订单不存在);
        //    if (oldFlightOrder.Status.Trim() != "已作废")
        //        throw new BusinessException(BusinessFail.飞机票订单没有作废);

        //    oldFlightOrder.VoidCause = string.Empty;
        //    oldFlightOrder.Status = "使用中";
        //    oldFlightOrder.UpdateTime = dateTime;

            

        //    foreach (FlightOrderDetail flightOrderDetail in oldFlightOrder.FlightOrderDetail.ToList())
        //    {
        //        //foreach (var insurancePolicy in flightOrderDetail.InsurancePolicy.ToList())
        //        //{
        //        //    insurancePolicy.Status = "已用";
        //        //    insurancePolicy.UsedDate = flightOrder.ProvideTicketDate;
        //        //    insurancePolicy.FlightDetailID = null;
        //        //    insurancePolicy.OrderDate = flightOrder.OrderDate;
        //        //    insurancePolicy.OrderUserID = null;
        //        //}

        //        //foreach (var journeyOrder in flightOrderDetail.JourneyOrder.ToList())
        //        //{
        //        //    journeyOrder.Status = "可用";
        //        //    journeyOrder.UsedDate = null;
        //        //    journeyOrder.FlightDetailID = null;
        //        //}

        //        if (flightOrderDetail.PaymentType != "现金")
        //        {
        //            BankAccount bankAccount = (from b in bankAccountList
        //                                       where b.ID == flightOrderDatail.BankAccount.ID
        //                                       select b).SingleOrDefault();
        //            decimal 余额 = bankAccount.Amount - flightOrderDatail.AccountPayable.Value;
        //            if (余额 < 0 && !bankAccount.CanNegative)
        //                throw new BusinessException(BusinessFail.余额不足);

        //            bankAccount.Amount = 余额;
        //        }

        //        VendorSettlement vendorSettlement = new VendorSettlement();
        //        vendorSettlement.ID = Guid.NewGuid();
        //        vendorSettlement.PayType = flightOrderDatail.PaymentType;
        //        vendorSettlement.PostingDate = flightOrder.OrderDate.Value;
        //        vendorSettlement.Ref = newFlightOrderDetail.TicketNo;
        //        vendorSettlement.Remark = string.Empty;
        //        vendorSettlement.Amount = flightOrderDatail.AccountPayable.Value;
        //        vendorSettlement.ExchangeRate = flightOrderDatail.ExchangeRate;
        //        vendorSettlement.CurrencyID = flightOrder.VendorCurrency.ID;
        //        if (flightOrderDatail.BankAccount != null)
        //            vendorSettlement.BankAccountID = flightOrderDatail.BankAccount.ID;
        //        vendorSettlement.CheckNo = string.Empty;
        //        vendorSettlement.SourceID = flightOrderDatail.ID;
        //        vendorSettlement.Type = flightOrder.OrderType;
        //        vendorSettlement.VendorID = flightOrderDatail.Vendor.ID;

        //        entities.AddToVendorSettlement(vendorSettlement);
        //        }

        //        entities.DeleteObject(vendorSettlement);
        //    }

        //    entities.SaveChanges();
        //}

        public void KeepFlightOrderDetail(Contract.FlightOrderDatail flightOrderDatail)
        {
            DateTime dateTime = DateTime.Now;

            FlightOrderDetail oldFlightOrderDetail = (from fod in entities.FlightOrderDetail
                                                      where fod.ID == flightOrderDatail.ID
                                                      select fod).SingleOrDefault();
            if (oldFlightOrderDetail == null)
                throw new BusinessException(BusinessFail.机票不存在);
            if (oldFlightOrderDetail.Status.Trim() == "留用")
                throw new BusinessException(BusinessFail.机票已经留用);
            if (oldFlightOrderDetail.Status.Trim() == "已退票")
                throw new BusinessException(BusinessFail.机票已经退票);

            oldFlightOrderDetail.Status = "留用";
            oldFlightOrderDetail.UpdateTime = dateTime;

            entities.SaveChanges();
        }

        public string ChangeFlightOrder(Contract.FlightChange flightChange, Contract.FlightOrderDatail flightOrderDatail, decimal profit)
        {
            checkFlightChange(flightChange);
            FinanceBL financeBL = new FinanceBL();
            DateTime dateTime = DateTime.Now;

            FlightOrderDetail dataFlightOrderDetail = (from fod in entities.FlightOrderDetail
                                                       where fod.ID == flightOrderDatail.ID
                                                       select fod).SingleOrDefault();
            if (dataFlightOrderDetail == null)
                throw new BusinessException(BusinessFail.机票不存在);
            if (dataFlightOrderDetail.Status.Trim() == "已退票")
                throw new BusinessException(BusinessFail.机票已经退票);

            dataFlightOrderDetail.AccountPayable = flightOrderDatail.AccountPayable.Value;
            dataFlightOrderDetail.AccountReceivable = flightOrderDatail.AccountReceivable;
            dataFlightOrderDetail.AirwaysOrderNo = flightOrderDatail.AirwaysOrderNo;
            dataFlightOrderDetail.Cost = flightOrderDatail.Cost;
            dataFlightOrderDetail.NetPrice = flightOrderDatail.NetPrice;
            dataFlightOrderDetail.PassengerID = flightOrderDatail.Passenger.ID;
            dataFlightOrderDetail.Price = flightOrderDatail.Price;
            dataFlightOrderDetail.Profit = flightOrderDatail.GetProfit(flightChange.ExchangeRate).Value;
            dataFlightOrderDetail.Remark = flightOrderDatail.Remark;
            dataFlightOrderDetail.Status = flightOrderDatail.Status;
            dataFlightOrderDetail.Tax1 = flightOrderDatail.Tax1;
            dataFlightOrderDetail.Tax2 = flightOrderDatail.Tax2;
            dataFlightOrderDetail.TicketNo = flightOrderDatail.TicketNo;
            dataFlightOrderDetail.VendorDiscount = flightOrderDatail.VendorDiscount;
            dataFlightOrderDetail.WayType = flightOrderDatail.WayType;
            dataFlightOrderDetail.UpdateTime = dateTime;

            foreach (Contract.InsurancePolicy insurancePolicy in flightOrderDatail.InsurancePolicyList)
            {
                InsurancePolicy dataInsurancePolicy = (from ip in entities.InsurancePolicy
                                                       where ip.ID == insurancePolicy.ID
                                                       select ip).SingleOrDefault();
                if (dataInsurancePolicy.FlightDetailID != flightOrderDatail.ID)
                {
                    dataInsurancePolicy.Status = "已用";
                    dataInsurancePolicy.UsedDate = dataFlightOrderDetail.FlightOrder.ProvideTicketDate;
                    dataInsurancePolicy.OrderDate = dateTime;
                    dataInsurancePolicy.OrderUserID = dataFlightOrderDetail.FlightOrder.OrderUserID;
                    dataInsurancePolicy.FlightDetailID = flightOrderDatail.ID;
                }
            }

            var insurancePolicyList = from ip in entities.InsurancePolicy
                                      where ip.FlightDetailID == flightOrderDatail.ID
                                      select ip;
            foreach (InsurancePolicy dataInsurancePolicy in insurancePolicyList)
            {
                Contract.InsurancePolicy insurancePolicy = flightOrderDatail.InsurancePolicyList.Find(ip => ip.ID == dataInsurancePolicy.ID);
                if (insurancePolicy == null)
                {
                    dataInsurancePolicy.Status = "可用";
                    dataInsurancePolicy.UsedDate = null;
                    dataInsurancePolicy.OrderDate = null;
                    dataInsurancePolicy.OrderUserID = null;
                    dataInsurancePolicy.FlightDetailID = null;
                }
            }

            dataFlightOrderDetail.InsuranceFee = flightOrderDatail.InsuranceFee;
            dataFlightOrderDetail.IsInsurancePolicyFree = flightOrderDatail.IsInsurancePolicyFree;

            foreach (Contract.JourneyOrder journeyOrder in flightOrderDatail.JourneyOrderList)
            {
                JourneyOrder dataJourneyOrder = (from ip in entities.JourneyOrder
                                                 where ip.ID == journeyOrder.ID
                                                 select ip).SingleOrDefault();

                if (dataJourneyOrder.FlightDetailID != flightOrderDatail.ID)
                {
                    dataJourneyOrder.Status = "已用";
                    dataJourneyOrder.UsedDate = dataFlightOrderDetail.FlightOrder.ProvideTicketDate;
                    dataJourneyOrder.FlightDetailID = flightOrderDatail.ID;
                }
            }

            var journeyOrderList = from ip in entities.JourneyOrder
                                   where ip.FlightDetailID == flightOrderDatail.ID
                                   select ip;
            foreach (JourneyOrder dataJourneyOrder in journeyOrderList)
            {
                Contract.JourneyOrder journeyOrder = flightOrderDatail.JourneyOrderList.Find(ip => ip.ID == dataJourneyOrder.ID);
                if (journeyOrder == null)
                {
                    dataJourneyOrder.Status = "可用";
                    dataJourneyOrder.UsedDate = null;
                    dataJourneyOrder.FlightDetailID = null;
                }
            }

            foreach (Contract.FlightWay flightWay in flightOrderDatail.FlightWayList)
            {
                FlightWay dataFlightWay = (from fw in entities.FlightWay
                                           where fw.ID == flightWay.ID
                                           select fw).SingleOrDefault();
                if (dataFlightWay == null)
                {
                    dataFlightWay = new FlightWay();
                    dataFlightWay.ID = flightWay.ID;
                    dataFlightWay.DepartDate = flightWay.DepartDate.Value;
                    dataFlightWay.DepartLocation = flightWay.DepartLocation;
                    dataFlightWay.Destination = flightWay.Destination;
                    dataFlightWay.FlightNo = flightWay.FlightNo;
                    dataFlightWay.FlightOrderDetailID = dataFlightOrderDetail.ID;
                    dataFlightWay.Seat = flightWay.Seat;
                    dataFlightWay.IsDelete = flightWay.IsDelete;
                    dataFlightWay.CreateTime = dateTime;
                    dataFlightWay.UpdateTime = dateTime;

                    entities.AddToFlightWay(dataFlightWay);
                }
                else
                {
                    dataFlightWay.DepartDate = flightWay.DepartDate.Value;
                    dataFlightWay.DepartLocation = flightWay.DepartLocation;
                    dataFlightWay.Destination = flightWay.Destination;
                    dataFlightWay.FlightNo = flightWay.FlightNo;
                    dataFlightWay.FlightOrderDetailID = dataFlightOrderDetail.ID;
                    dataFlightWay.Seat = flightWay.Seat;
                    dataFlightWay.IsDelete = flightWay.IsDelete;
                    dataFlightWay.UpdateTime = dateTime;
                }
            }

            FlightChange newFlightChange = new FlightChange();
            newFlightChange.ID = flightChange.ID;
            newFlightChange.Ref = new BaseBL().GetNewNr("FlightChange");
            newFlightChange.CustomerID = flightChange.Customer.ID;
            newFlightChange.CurrencyID = flightChange.Currency.ID;
            newFlightChange.ExchangeRate = flightChange.ExchangeRate;
            newFlightChange.FlightOrderDetailID = flightOrderDatail.ID;
            newFlightChange.OrderDate = flightChange.OrderDate.Value;
            newFlightChange.OrderUserID = flightChange.OrderUser.ID;
            newFlightChange.Price = flightChange.Price;
            newFlightChange.Remark = flightChange.Remark;
            newFlightChange.VendorCurrencyID = flightChange.VendorCurrency.ID;
            newFlightChange.VendorID = flightChange.Vendor.ID;
            newFlightChange.VendorPrice = flightChange.VendorPrice;
            newFlightChange.CustomerStatus = "未付款";
            newFlightChange.VendorStatus = "已付款";
            newFlightChange.CreateTime = dateTime;
            newFlightChange.UpdateTime = dateTime;

            entities.AddToFlightChange(newFlightChange);

            FlightOrder flightOrder = (from fo in entities.FlightOrder
                                       where fo.ID == dataFlightOrderDetail.FlightOrderID
                                       select fo).Single();
            flightOrder.TotalProfit = profit;

            if (flightChange.PaymentType != "现金")
            {
                BankAccount bankAccount = (from b in entities.BankAccount
                                           where b.ID == flightChange.BankAccount.ID
                                           select b).SingleOrDefault();
                decimal 余额 = bankAccount.Amount - flightChange.VendorPrice;
                if (余额 < 0 && !bankAccount.CanNegative)
                    throw new BusinessException(BusinessFail.余额不足);

                bankAccount.Amount = 余额;
            }
            else
            {
                decimal cash = financeBL.GetCash();
                financeBL.SetCash(cash - flightChange.VendorPrice, false);
            }

            VendorSettlement foVendorSettlement = (from vs in entities.VendorSettlement
                                                   where vs.SourceID == flightOrderDatail.ID
                                                   select vs).SingleOrDefault();

            VendorSettlement vendorSettlement = new VendorSettlement();
            vendorSettlement.ID = Guid.NewGuid();
            vendorSettlement.PayType = flightChange.PaymentType;
            vendorSettlement.PostingDate = flightChange.OrderDate.Value;
            vendorSettlement.Ref = flightChange.Ref;
            vendorSettlement.Remark = string.Empty;
            vendorSettlement.Amount = flightChange.VendorPrice;
            vendorSettlement.ExchangeRate = foVendorSettlement.ExchangeRate;
            vendorSettlement.CurrencyID = foVendorSettlement.CurrencyID;
            if (flightChange.BankAccount != null)
                vendorSettlement.BankAccountID = flightChange.BankAccount.ID;
            vendorSettlement.CheckNo = string.Empty;
            vendorSettlement.SourceID = flightChange.ID;
            vendorSettlement.Type = flightChange.OrderType;
            vendorSettlement.VendorID = flightChange.Vendor.ID;

            entities.AddToVendorSettlement(vendorSettlement);

            financeBL.Save();
            entities.SaveChanges();

            return newFlightChange.Ref;
        }

        private void checkFlightChange(Contract.FlightChange flightChange)
        {
            flightChange.Remark = flightChange.Remark.Trim();

            if (flightChange.Currency != flightChange.VendorCurrency && flightChange.ExchangeRate == 0)
                throw new BusinessException(VerificationText.请输入汇率);
            if (flightChange.ExchangeRate < 0)
                throw new BusinessException(VerificationText.汇率格式不正确);
        }

        public string ReturnFlightOrder(Contract.FlightReturn flightReturn, Contract.FlightOrderDatail flightOrderDatail, decimal profit)
        {
            checkFlightReturn(flightReturn);
            FinanceBL financeBL = new FinanceBL();
            flightReturn.FlightOrderDetail = flightOrderDatail;
            DateTime dateTime = DateTime.Now;

            FlightOrderDetail dataFlightOrderDetail = (from fod in entities.FlightOrderDetail
                                                       where fod.ID == flightOrderDatail.ID
                                                       select fod).SingleOrDefault();
            if (dataFlightOrderDetail == null)
                throw new BusinessException(BusinessFail.机票不存在);
            if (dataFlightOrderDetail.Status.Trim() == "已退票")
                throw new BusinessException(BusinessFail.机票已经退票);

            dataFlightOrderDetail.Status = "已退票";
            dataFlightOrderDetail.UpdateTime = dateTime;
            foreach (var journeyOrder in dataFlightOrderDetail.JourneyOrder.ToList())
            {
                journeyOrder.Status = "作废";
                //journeyOrder.UsedDate = null;
                //journeyOrder.FlightDetailID = null;
            }

            FlightReturn newFlightReturn = new FlightReturn();
            newFlightReturn.ID = flightReturn.ID;
            newFlightReturn.Ref = new BaseBL().GetNewNr("FlightReturn");
            newFlightReturn.CurrencyID = flightReturn.Currency.ID;
            newFlightReturn.CustomerReturnPrice = flightReturn.CustomerReturnPrice;
            newFlightReturn.CustomerUsedPrice = flightReturn.CustomerUsedPrice;
            newFlightReturn.ExchangeRate = flightReturn.ExchangeRate;
            newFlightReturn.FlightOrderDetailID = flightOrderDatail.ID;
            newFlightReturn.OrderDate = flightReturn.OrderDate.Value;
            newFlightReturn.OrderUserID = flightReturn.OrderUser.ID;
            newFlightReturn.Remark = flightReturn.Remark;
            newFlightReturn.UsedTax = flightReturn.UsedTax;
            newFlightReturn.VendorCurrencyID = flightReturn.VendorCurrency.ID;
            newFlightReturn.VendorID = flightReturn.Vendor.ID;
            newFlightReturn.CustomerID = flightReturn.Customer.ID;
            newFlightReturn.VendorReturnPrice = flightReturn.VendorReturnPrice;
            newFlightReturn.VendorUsedPrice = flightReturn.VendorUsedPrice;
            newFlightReturn.CustomerReturnAmount = flightReturn.CustomerReturnAmount.Value;
            newFlightReturn.VendorReturnAmount = flightReturn.VendorReturnAmount.Value;
            newFlightReturn.Profit = flightReturn.Profit.Value;
            newFlightReturn.Intergral = flightReturn.GetIntergral(flightReturn.Customer).Value;
            newFlightReturn.CustomerStatus = "已付款";
            newFlightReturn.VendorStatus = "未付款";
            newFlightReturn.CreateTime = dateTime;
            newFlightReturn.UpdateTime = dateTime;

            entities.AddToFlightReturn(newFlightReturn);

            FlightOrder flightOrder = (from fo in entities.FlightOrder
                                       where fo.ID == dataFlightOrderDetail.FlightOrderID
                                       select fo).Single();
            flightOrder.TotalProfit = profit;

            if (flightReturn.PaymentType != "现金")
            {
                BankAccount bankAccount = (from b in entities.BankAccount
                                           where b.ID == flightReturn.BankAccount.ID
                                           select b).SingleOrDefault();
                decimal 余额 = bankAccount.Amount - flightReturn.VendorReturnAmount.Value;
                if (余额 < 0 && !bankAccount.CanNegative)
                    throw new BusinessException(BusinessFail.余额不足);

                bankAccount.Amount = 余额;
            }
            else
            {
                decimal cash = financeBL.GetCash();
                financeBL.SetCash(cash - flightReturn.VendorReturnAmount.Value, false);
            }

            VendorSettlement foVendorSettlement = (from vs in entities.VendorSettlement
                                                   where vs.SourceID == flightOrderDatail.ID
                                                   select vs).SingleOrDefault();

            VendorSettlement vendorSettlement = new VendorSettlement();
            vendorSettlement.ID = Guid.NewGuid();
            vendorSettlement.PayType = flightReturn.PaymentType;
            vendorSettlement.PostingDate = flightReturn.OrderDate.Value;
            vendorSettlement.Ref = flightReturn.Ref;
            vendorSettlement.Remark = string.Empty;
            vendorSettlement.Amount = 0 - flightReturn.VendorReturnAmount.Value;
            vendorSettlement.ExchangeRate = foVendorSettlement.ExchangeRate;
            vendorSettlement.CurrencyID = foVendorSettlement.CurrencyID;
            if (flightReturn.BankAccount != null)
                vendorSettlement.BankAccountID = flightReturn.BankAccount.ID;
            vendorSettlement.CheckNo = string.Empty;
            vendorSettlement.SourceID = flightReturn.ID;
            vendorSettlement.Type = flightReturn.OrderType;
            vendorSettlement.VendorID = flightReturn.Vendor.ID;

            entities.AddToVendorSettlement(vendorSettlement);

            entities.SaveChanges();
            financeBL.Save();

            return newFlightReturn.Ref;
        }

        private void checkFlightReturn(Contract.FlightReturn flightReturn)
        {
            flightReturn.Remark = flightReturn.Remark.Trim();

            if (flightReturn.Currency != flightReturn.VendorCurrency && flightReturn.ExchangeRate == 0)
                throw new BusinessException(VerificationText.请输入汇率);
            if (flightReturn.ExchangeRate < 0)
                throw new BusinessException(VerificationText.汇率格式不正确);
        }

        public string NewTrainTicketOrder(Contract.TrainTicketOrder trainTicketOrder)
        {
            checkTrainTicketOrder(trainTicketOrder);

            DateTime dateTime = DateTime.Now;

            TrainTicketOrder newTrainTicketOrder = new TrainTicketOrder();
            newTrainTicketOrder.ID = trainTicketOrder.ID;
            newTrainTicketOrder.Ref = new BaseBL().GetNewNr("Train");
            newTrainTicketOrder.Address = trainTicketOrder.Address;
            newTrainTicketOrder.ContactPerson = trainTicketOrder.ContactPerson;
            newTrainTicketOrder.ContactPersonTel = trainTicketOrder.ContactPersonTel;
            newTrainTicketOrder.CreditTerm = trainTicketOrder.CreditTerm;
            newTrainTicketOrder.CurrencyID = trainTicketOrder.Currency.ID;
            newTrainTicketOrder.CustomerID = trainTicketOrder.Customer.ID;
            newTrainTicketOrder.DeliveryDate = trainTicketOrder.DeliveryDate;
            newTrainTicketOrder.ExchangeRate = trainTicketOrder.ExchangeRate;
            newTrainTicketOrder.OrderDate = trainTicketOrder.OrderDate.Value;
            newTrainTicketOrder.OrderTicketUserID = trainTicketOrder.OrderTicketUser.ID;
            newTrainTicketOrder.OrderUserID = trainTicketOrder.OrderUser.ID;
            newTrainTicketOrder.PrintTotalPrice = trainTicketOrder.PrintTotalPrice;
            newTrainTicketOrder.ProvideTicketDate = trainTicketOrder.ProvideTicketDate;
            newTrainTicketOrder.ProvideTicketUserID = trainTicketOrder.ProvideTicketUser.ID;
            newTrainTicketOrder.Remark = trainTicketOrder.Remark;
            newTrainTicketOrder.SalesmanID = trainTicketOrder.Salesman.ID;
            newTrainTicketOrder.Status = trainTicketOrder.Status;
            newTrainTicketOrder.TicketTotalQty = trainTicketOrder.TicketTotalQty;
            newTrainTicketOrder.TotalPrice = trainTicketOrder.TotalPrice.Value;
            newTrainTicketOrder.TotalVendorPrice = trainTicketOrder.TotalVendorPrice.Value;
            newTrainTicketOrder.TotalProfit = trainTicketOrder.TotalProfit.Value;
            newTrainTicketOrder.VendorCurrencyID = trainTicketOrder.VendorCurrency.ID;
            newTrainTicketOrder.VendorID = trainTicketOrder.Vendor.ID;
            newTrainTicketOrder.VoidCause = null;
            newTrainTicketOrder.CustomerStatus = trainTicketOrder.CustomerStatus;
            newTrainTicketOrder.VendorStatus = "未付款";
            newTrainTicketOrder.CreateTime = dateTime;
            newTrainTicketOrder.UpdateTime = dateTime;

            entities.AddToTrainTicketOrder(newTrainTicketOrder);

            foreach (Contract.TrainTicketOrderDetail trainTicketOrderDetail in trainTicketOrder.Details)
            {
                TrainTicketOrderDetail newTrainTicketOrderDetail = new TrainTicketOrderDetail();
                newTrainTicketOrderDetail.ID = trainTicketOrderDetail.ID;
                newTrainTicketOrderDetail.DeliveryCost = trainTicketOrderDetail.DeliveryCost;
                newTrainTicketOrderDetail.DepartDate = convertToDateTime(trainTicketOrderDetail.DepartDate.Value, trainTicketOrderDetail.出发时间);
                newTrainTicketOrderDetail.DepartLocation = trainTicketOrderDetail.DepartLocation;
                newTrainTicketOrderDetail.Destination = trainTicketOrderDetail.Destination;
                newTrainTicketOrderDetail.IsToHK = trainTicketOrderDetail.IsToHK;
                newTrainTicketOrderDetail.TicketPrice = trainTicketOrderDetail.TicketPrice;
                newTrainTicketOrderDetail.Profit = trainTicketOrderDetail.Profit.Value;
                newTrainTicketOrderDetail.Qty = trainTicketOrderDetail.Qty;
                newTrainTicketOrderDetail.SeatType = trainTicketOrderDetail.SeatType;
                newTrainTicketOrderDetail.Status = trainTicketOrder.Status;
                newTrainTicketOrderDetail.TotalPrice = trainTicketOrderDetail.TotalPrice.Value;
                newTrainTicketOrderDetail.TrainNo = trainTicketOrderDetail.TrainNo;
                newTrainTicketOrderDetail.TrainTicketOrderID = newTrainTicketOrder.ID;
                newTrainTicketOrderDetail.VendorPrice = trainTicketOrderDetail.VendorPrice;
                newTrainTicketOrderDetail.CreateTime = dateTime;
                newTrainTicketOrderDetail.UpdateTime = dateTime;

                entities.AddToTrainTicketOrderDetail(newTrainTicketOrderDetail);
            }

            //VendorSettlement vendorSettlement = new VendorSettlement();

            //if (trainTicketOrder.PaymentType != "现金")
            //{
            //    if (trainTicketOrder.BankAccount != null)
            //    {
            //        BankAccount bankAccount = (from b in entities.BankAccount
            //                                   where b.ID == trainTicketOrder.BankAccount.ID
            //                                   select b).SingleOrDefault();
            //        decimal 余额 = bankAccount.Amount - trainTicketOrder.TotalVendorPrice.Value;
            //        if (余额 < 0 && !bankAccount.CanNegative)
            //            throw new BusinessException(BusinessFail.余额不足);

            //        bankAccount.Amount = 余额;

            //        vendorSettlement.BankAccountID = trainTicketOrder.BankAccount.ID;
            //    }
            //}

            //vendorSettlement.ID = Guid.NewGuid();
            //vendorSettlement.PayType = trainTicketOrder.PaymentType;
            //vendorSettlement.PostingDate = trainTicketOrder.OrderDate.Value;
            //vendorSettlement.Ref = newTrainTicketOrder.Ref;
            //vendorSettlement.Remark = string.Empty;
            //vendorSettlement.Amount = trainTicketOrder.TotalVendorPrice.Value;
            //vendorSettlement.ExchangeRate = 1;
            //vendorSettlement.CurrencyID = trainTicketOrder.Currency.ID;
            //vendorSettlement.CheckNo = string.Empty;
            //vendorSettlement.SourceID = trainTicketOrder.ID;
            //vendorSettlement.Type = trainTicketOrder.OrderType;
            //vendorSettlement.VendorID = trainTicketOrder.Vendor.ID;

            //entities.AddToVendorSettlement(vendorSettlement);

            entities.SaveChanges();

            return newTrainTicketOrder.Ref;
        }

        private void checkTrainTicketOrder(Contract.TrainTicketOrder trainTicketOrder)
        {
            //trainTicketOrder.Address = trainTicketOrder.Address.Trim();
            trainTicketOrder.ContactPerson = trainTicketOrder.ContactPerson.Trim();
            trainTicketOrder.ContactPersonTel = trainTicketOrder.ContactPersonTel.Trim();
            trainTicketOrder.CreditTerm = trainTicketOrder.CreditTerm.Trim();
            trainTicketOrder.Remark = trainTicketOrder.Remark.Trim();

            //if (trainTicketOrder.Address == string.Empty)
            //    throw new BusinessException(VerificationText.请输入地址);
            if (trainTicketOrder.ContactPerson == string.Empty)
                throw new BusinessException(VerificationText.请输入联系人);
            if (trainTicketOrder.ContactPersonTel == string.Empty)
                throw new BusinessException(VerificationText.请输入联系人电话);
            if (trainTicketOrder.CreditTerm == string.Empty)
                throw new BusinessException(VerificationText.请输入客户结算方式);
            if (trainTicketOrder.Customer == null)
                throw new BusinessException(VerificationText.请输入客户);
            if (trainTicketOrder.OrderDate == null)
                throw new BusinessException(VerificationText.请输入开单日期);
            if (trainTicketOrder.OrderTicketUser == null)
                throw new BusinessException(VerificationText.请输入订票人);
            if (trainTicketOrder.OrderUser == null)
                throw new BusinessException(VerificationText.请输入经办人);
            if (trainTicketOrder.Vendor == null)
                throw new BusinessException(VerificationText.请输入供应商);
            if (trainTicketOrder.VendorCurrency == null)
                throw new BusinessException(VerificationText.请输入货币);
            if (trainTicketOrder.ExchangeRate == 0)
                throw new BusinessException(VerificationText.请输入汇率);
            if (trainTicketOrder.ExchangeRate < 0)
                throw new BusinessException(VerificationText.汇率格式不正确);
            if (trainTicketOrder.Details.Count == 0)
                throw new BusinessException(VerificationText.请输入火车票订单明细);

            foreach (Contract.TrainTicketOrderDetail trainTicketOrderDetail in trainTicketOrder.Details)
            {
                checkTrainTicketOrderDetail(trainTicketOrderDetail);
            }
        }

        private void checkTrainTicketOrderDetail(Contract.TrainTicketOrderDetail trainTicketOrderDetail)
        {
            trainTicketOrderDetail.DepartLocation = trainTicketOrderDetail.DepartLocation.Trim();
            trainTicketOrderDetail.Destination = trainTicketOrderDetail.Destination.Trim();
            trainTicketOrderDetail.SeatType = trainTicketOrderDetail.SeatType.Trim();
            trainTicketOrderDetail.TrainNo = trainTicketOrderDetail.TrainNo.Trim();

            if (trainTicketOrderDetail.DepartLocation == string.Empty)
                throw new BusinessException(VerificationText.请输入出发地);
            if (trainTicketOrderDetail.Destination == string.Empty)
                throw new BusinessException(VerificationText.请输入目的地);
            if (trainTicketOrderDetail.DepartDate == null)
                throw new BusinessException(VerificationText.请输入出发日期);
            if (trainTicketOrderDetail.TrainNo == string.Empty)
                throw new BusinessException(VerificationText.请输入车次);
            if (trainTicketOrderDetail.SeatType == string.Empty)
                throw new BusinessException(VerificationText.请输入座位类型);
            if (trainTicketOrderDetail.DeliveryCost == null)
                throw new BusinessException(VerificationText.请输入结算价);
            if (trainTicketOrderDetail.VendorPrice == null)
                throw new BusinessException(VerificationText.请输入底价);
        }

        public string UpdateTrainTicketOrder(Contract.TrainTicketOrder trainTicketOrder)
        {
            foreach (Contract.TrainTicketOrderDetail trainTicketOrderDetail in trainTicketOrder.Details)
                trainTicketOrderDetail.TrainTicketOrder = trainTicketOrder;

            checkTrainTicketOrder(trainTicketOrder);

            DateTime dateTime = DateTime.Now;

            TrainTicketOrder dataTrainTicketOrder = (from tto in entities.TrainTicketOrder
                                                     where tto.ID == trainTicketOrder.ID
                                                     select tto).SingleOrDefault();
            if (dataTrainTicketOrder == null)
            {
                return NewTrainTicketOrder(trainTicketOrder);
            }

            dataTrainTicketOrder.Address = trainTicketOrder.Address;
            dataTrainTicketOrder.ContactPerson = trainTicketOrder.ContactPerson;
            dataTrainTicketOrder.ContactPersonTel = trainTicketOrder.ContactPersonTel;
            dataTrainTicketOrder.CreditTerm = trainTicketOrder.CreditTerm;
            dataTrainTicketOrder.CurrencyID = trainTicketOrder.Currency.ID;
            dataTrainTicketOrder.CustomerID = trainTicketOrder.Customer.ID;
            dataTrainTicketOrder.DeliveryDate = trainTicketOrder.DeliveryDate;
            dataTrainTicketOrder.ExchangeRate = trainTicketOrder.ExchangeRate;
            dataTrainTicketOrder.OrderDate = trainTicketOrder.OrderDate.Value;
            dataTrainTicketOrder.OrderTicketUserID = trainTicketOrder.OrderTicketUser.ID;
            dataTrainTicketOrder.OrderUserID = trainTicketOrder.OrderUser.ID;
            dataTrainTicketOrder.PrintTotalPrice = trainTicketOrder.PrintTotalPrice;
            dataTrainTicketOrder.ProvideTicketDate = trainTicketOrder.ProvideTicketDate;
            dataTrainTicketOrder.ProvideTicketUserID = trainTicketOrder.ProvideTicketUser.ID;
            dataTrainTicketOrder.Remark = trainTicketOrder.Remark;
            dataTrainTicketOrder.SalesmanID = trainTicketOrder.Salesman.ID;
            dataTrainTicketOrder.Status = trainTicketOrder.Status;
            dataTrainTicketOrder.TicketTotalQty = trainTicketOrder.TicketTotalQty;
            dataTrainTicketOrder.TotalPrice = trainTicketOrder.TotalPrice.Value;
            dataTrainTicketOrder.TotalVendorPrice = trainTicketOrder.TotalVendorPrice.Value;
            dataTrainTicketOrder.TotalProfit = trainTicketOrder.TotalProfit.Value;
            dataTrainTicketOrder.VendorCurrencyID = trainTicketOrder.VendorCurrency.ID;
            dataTrainTicketOrder.VendorID = trainTicketOrder.Vendor.ID;
            dataTrainTicketOrder.VoidCause = null;
            dataTrainTicketOrder.UpdateTime = dateTime;

            foreach (Contract.TrainTicketOrderDetail trainTicketOrderDetail in trainTicketOrder.Details)
            {
                TrainTicketOrderDetail dataTrainTicketOrderDetail = (from ttod in entities.TrainTicketOrderDetail
                                                                     where ttod.ID == trainTicketOrderDetail.ID
                                                                     select ttod).SingleOrDefault();
                if (dataTrainTicketOrderDetail == null)
                {
                    dataTrainTicketOrderDetail = new TrainTicketOrderDetail();
                    dataTrainTicketOrderDetail.ID = trainTicketOrderDetail.ID;
                    dataTrainTicketOrderDetail.DeliveryCost = trainTicketOrderDetail.DeliveryCost;
                    dataTrainTicketOrderDetail.DepartDate = convertToDateTime(trainTicketOrderDetail.DepartDate.Value, trainTicketOrderDetail.出发时间);
                    dataTrainTicketOrderDetail.DepartLocation = trainTicketOrderDetail.DepartLocation;
                    dataTrainTicketOrderDetail.Destination = trainTicketOrderDetail.Destination;
                    dataTrainTicketOrderDetail.IsToHK = trainTicketOrderDetail.IsToHK;
                    dataTrainTicketOrderDetail.Profit = trainTicketOrderDetail.Profit.Value;
                    dataTrainTicketOrderDetail.TicketPrice = trainTicketOrderDetail.TicketPrice;
                    dataTrainTicketOrderDetail.Qty = trainTicketOrderDetail.Qty;
                    dataTrainTicketOrderDetail.SeatType = trainTicketOrderDetail.SeatType;
                    dataTrainTicketOrderDetail.Status = trainTicketOrder.Status;
                    dataTrainTicketOrderDetail.TotalPrice = trainTicketOrderDetail.TotalPrice.Value;
                    dataTrainTicketOrderDetail.TrainNo = trainTicketOrderDetail.TrainNo;
                    dataTrainTicketOrderDetail.TrainTicketOrderID = dataTrainTicketOrder.ID;
                    dataTrainTicketOrderDetail.VendorPrice = trainTicketOrderDetail.VendorPrice;
                    dataTrainTicketOrderDetail.Status = trainTicketOrderDetail.Status;
                    dataTrainTicketOrderDetail.CreateTime = dateTime;
                    dataTrainTicketOrderDetail.UpdateTime = dateTime;

                    entities.AddToTrainTicketOrderDetail(dataTrainTicketOrderDetail);
                }
                else
                {
                    dataTrainTicketOrderDetail.DeliveryCost = trainTicketOrderDetail.DeliveryCost;
                    dataTrainTicketOrderDetail.DepartDate = convertToDateTime(trainTicketOrderDetail.DepartDate.Value, trainTicketOrderDetail.出发时间);
                    dataTrainTicketOrderDetail.DepartLocation = trainTicketOrderDetail.DepartLocation;
                    dataTrainTicketOrderDetail.Destination = trainTicketOrderDetail.Destination;
                    dataTrainTicketOrderDetail.IsToHK = trainTicketOrderDetail.IsToHK;
                    dataTrainTicketOrderDetail.TicketPrice = trainTicketOrderDetail.TicketPrice;
                    dataTrainTicketOrderDetail.Profit = trainTicketOrderDetail.Profit.Value;
                    dataTrainTicketOrderDetail.Qty = trainTicketOrderDetail.Qty;
                    dataTrainTicketOrderDetail.SeatType = trainTicketOrderDetail.SeatType;
                    dataTrainTicketOrderDetail.TotalPrice = trainTicketOrderDetail.TotalPrice.Value;
                    dataTrainTicketOrderDetail.TrainNo = trainTicketOrderDetail.TrainNo;
                    dataTrainTicketOrderDetail.TrainTicketOrderID = dataTrainTicketOrder.ID;
                    dataTrainTicketOrderDetail.VendorPrice = trainTicketOrderDetail.VendorPrice;
                    dataTrainTicketOrderDetail.Status = trainTicketOrderDetail.Status;
                    dataTrainTicketOrderDetail.UpdateTime = dateTime;
                }
            }

            entities.SaveChanges();

            return dataTrainTicketOrder.Ref;
        }

        public void VoidTrainTicketOrder(Contract.TrainTicketOrder trainTicketOrder, string cause)
        {
            cause = cause.Trim();

            DateTime dateTime = DateTime.Now;

            TrainTicketOrder dataTrainTicketOrder = (from tto in entities.TrainTicketOrder
                                                     where tto.ID == trainTicketOrder.ID
                                                     select tto).SingleOrDefault();
            if (dataTrainTicketOrder == null)
                throw new BusinessException(BusinessFail.火车票订单不存在);
            if (dataTrainTicketOrder.Status.Trim() == "已作废")
                throw new BusinessException(BusinessFail.火车票订单已作废);
            if (dataTrainTicketOrder.CustomerStatus.Trim() == "已付款")
                throw new BusinessException(BusinessFail.客户已付款);

            dataTrainTicketOrder.VoidCause = cause;
            dataTrainTicketOrder.Status = "已作废";
            dataTrainTicketOrder.UpdateTime = dateTime;

            VendorSettlement vendorSettlement = (from vs in entities.VendorSettlement
                                                 where vs.SourceID == trainTicketOrder.ID
                                                 select vs).SingleOrDefault();
            if (vendorSettlement != null)
            {
                if (vendorSettlement.PayType != "现金")
                {
                    if (vendorSettlement.BankAccountID != null)
                    {
                        BankAccount bankAccount = (from b in entities.BankAccount
                                                   where b.ID == vendorSettlement.BankAccount.ID
                                                   select b).SingleOrDefault();

                        bankAccount.Amount += vendorSettlement.Amount;
                    }
                }

                entities.DeleteObject(vendorSettlement);
            }
            entities.SaveChanges();
        }

        public string ReturnTrainTicketOrder(Contract.TrainTicketReturn trainTicketReturn, Contract.TrainTicketOrderDetail trainTicketOrderDetail, decimal profit)
        {
            checkTrainTicketReturn(trainTicketReturn);

            DateTime dateTime = DateTime.Now;

            TrainTicketOrderDetail dataTrainTicketOrderDetail = (from ttod in entities.TrainTicketOrderDetail
                                                                 where ttod.ID == trainTicketOrderDetail.ID
                                                                 select ttod).SingleOrDefault();
            if (dataTrainTicketOrderDetail == null)
                throw new BusinessException(BusinessFail.火车票不存在);
            if (dataTrainTicketOrderDetail.Status.Trim() == "已退票")
                throw new BusinessException(BusinessFail.火车票已经退票);

            dataTrainTicketOrderDetail.Status = "已退票";
            dataTrainTicketOrderDetail.UpdateTime = dateTime;

            TrainTicketReturn newTrainTicketReturn = new TrainTicketReturn();
            newTrainTicketReturn.ID = trainTicketReturn.ID;
            newTrainTicketReturn.TrainTicketOrderDetailID = trainTicketOrderDetail.ID;
            newTrainTicketReturn.Ref = new BaseBL().GetNewNr("TrainReturn");
            newTrainTicketReturn.CurrencyID = trainTicketReturn.Currency.ID;
            newTrainTicketReturn.OrderDate = trainTicketReturn.OrderDate.Value;
            newTrainTicketReturn.OrderUserID = trainTicketReturn.OrderUser.ID;
            newTrainTicketReturn.Qty = trainTicketReturn.Qty;
            newTrainTicketReturn.ServiceCharge = trainTicketReturn.ServiceCharge;
            newTrainTicketReturn.TrainTicketOrderDetailID = dataTrainTicketOrderDetail.ID;
            newTrainTicketReturn.VendorServiceCharge = trainTicketReturn.VendorServiceCharge;
            newTrainTicketReturn.CustomerStatus = "已付款";
            newTrainTicketReturn.VendorStatus = "未付款";
            newTrainTicketReturn.VendorID = trainTicketReturn.Vendor.ID;
            newTrainTicketReturn.CustomerID = trainTicketReturn.Customer.ID;
            newTrainTicketReturn.VendorCurrencyID = trainTicketReturn.VendorCurrency.ID;
            newTrainTicketReturn.ExchangeRate = trainTicketReturn.ExchangeRate;
            newTrainTicketReturn.Profit = trainTicketReturn.Profit;
            newTrainTicketReturn.CreateTime = dateTime;
            newTrainTicketReturn.UpdateTime = dateTime;

            entities.AddToTrainTicketReturn(newTrainTicketReturn);

            TrainTicketOrder trainTicketOrder = (from fo in entities.TrainTicketOrder
                                                 where fo.ID == dataTrainTicketOrderDetail.TrainTicketOrderID
                                                 select fo).Single();
            trainTicketOrder.TotalProfit = profit;

            VendorSettlement ttoVendorSettlement = (from vs in entities.VendorSettlement
                                                    where vs.SourceID == trainTicketOrder.ID
                                                    select vs).SingleOrDefault();
            if (ttoVendorSettlement != null)
            {
                VendorSettlement vendorSettlement = new VendorSettlement();
                vendorSettlement.ID = Guid.NewGuid();
                vendorSettlement.PayType = ttoVendorSettlement.PayType;
                vendorSettlement.PostingDate = trainTicketReturn.OrderDate.Value;
                vendorSettlement.Ref = string.Empty;
                vendorSettlement.Remark = string.Empty;
                vendorSettlement.Amount = 0 - trainTicketReturn.VendorReturn;
                vendorSettlement.ExchangeRate = ttoVendorSettlement.ExchangeRate;
                vendorSettlement.CurrencyID = ttoVendorSettlement.CurrencyID;
                vendorSettlement.BankAccountID = ttoVendorSettlement.BankAccountID;
                vendorSettlement.CheckNo = string.Empty;
                vendorSettlement.SourceID = trainTicketReturn.ID;
                vendorSettlement.Type = trainTicketReturn.OrderType;
                vendorSettlement.VendorID = trainTicketReturn.Vendor.ID;

                entities.AddToVendorSettlement(vendorSettlement);
            }

            entities.SaveChanges();

            return newTrainTicketReturn.Ref;
        }

        private void checkTrainTicketReturn(Contract.TrainTicketReturn trainTicketReturn)
        {
            trainTicketReturn.Remark = trainTicketReturn.Remark.Trim();
        }


        public string NewHotelBooking(Contract.HotelBooking hotelBooking)
        {
            checkHotelBooking(hotelBooking);

            DateTime dateTime = DateTime.Now;


            HotelBooking newHotelBooking = new HotelBooking();
            newHotelBooking.ID = hotelBooking.ID;
            newHotelBooking.Ref = new BaseBL().GetNewNr("Hotel");
            newHotelBooking.Address = hotelBooking.Address.Trim();
            newHotelBooking.ContactPerson = hotelBooking.ContactPerson;
            newHotelBooking.ContactPersonTel = hotelBooking.ContactPersonTel;
            newHotelBooking.CurrencyID = hotelBooking.Currency.ID;
            newHotelBooking.CustomerID = hotelBooking.Customer.ID;
            newHotelBooking.Intergral = hotelBooking.TotalIntergral.Value;
            newHotelBooking.OrderDate = hotelBooking.OrderDate.Value;
            newHotelBooking.OrderHotelUserID = hotelBooking.OrderHotelUser.ID;
            newHotelBooking.OrderUserID = hotelBooking.OrderUser.ID;
            newHotelBooking.PaymentType = hotelBooking.CustomerPaymentType;
            newHotelBooking.PrintTotalPrice = hotelBooking.PrintTotalPrice;
            newHotelBooking.Remark = hotelBooking.Remark;
            newHotelBooking.RoomTotalQty = hotelBooking.RoomTotalQty;
            newHotelBooking.SalesmanID = hotelBooking.Salesman.ID;
            newHotelBooking.Status = hotelBooking.Status;
            newHotelBooking.DeliveryDate = hotelBooking.DeliveryDate;
            newHotelBooking.ProvideTicketDate = hotelBooking.ProvideTicketDate;
            newHotelBooking.ProvideTicketUserID = hotelBooking.ProvideTicketUser.ID;
            newHotelBooking.TotalPrice = hotelBooking.TotalPrice.Value;
            newHotelBooking.ExchangeRate = hotelBooking.ExchangeRate;
            newHotelBooking.TotalVendorPrice = hotelBooking.TotalVendorPrice.Value;
            newHotelBooking.TotalProfit = hotelBooking.TotalProfit.Value;
            newHotelBooking.VendorID = hotelBooking.Vendor.ID;
            newHotelBooking.VendorCurrencyID = hotelBooking.VendorCurrency.ID;
            newHotelBooking.VoidCause = null;
            newHotelBooking.CustomerStatus = hotelBooking.CustomerStatus;
            newHotelBooking.VendorStatus = "未付款";
            newHotelBooking.CreateTime = dateTime;
            newHotelBooking.UpdateTime = dateTime;

            entities.AddToHotelBooking(newHotelBooking);

            foreach (Contract.HotelBookingDetail hotelBookingDetail in hotelBooking.Details)
            {
                HotelBookingDetail newHotelBookingDetail = new HotelBookingDetail();
                newHotelBookingDetail.ID = hotelBookingDetail.ID;
                newHotelBookingDetail.CheckInDate = hotelBookingDetail.CheckInDate.Value;
                newHotelBookingDetail.CheckOutDate = hotelBookingDetail.CheckOutDate.Value;
                newHotelBookingDetail.Days = hotelBookingDetail.Days;
                newHotelBookingDetail.GuestName = hotelBookingDetail.GuestName;
                newHotelBookingDetail.HotelBookingID = hotelBooking.ID;
                newHotelBookingDetail.HotelConfirmationRef = hotelBookingDetail.HotelConfirmationRef;
                newHotelBookingDetail.HotelLevel = hotelBookingDetail.HotelLeave;
                newHotelBookingDetail.HotelName = hotelBookingDetail.HotelName;
                newHotelBookingDetail.HotelOrderRef = hotelBookingDetail.HotelOrderRef;
                newHotelBookingDetail.HotelTax = hotelBookingDetail.HotelTax;
                newHotelBookingDetail.Intergral = hotelBookingDetail.Intergral;
                newHotelBookingDetail.Price = hotelBookingDetail.Price;
                newHotelBookingDetail.Profit = hotelBookingDetail.Profit.Value;
                newHotelBookingDetail.Rooms = hotelBookingDetail.Rooms;
                newHotelBookingDetail.RoomType = hotelBookingDetail.RoomType;
                newHotelBookingDetail.TotalPrice = hotelBookingDetail.TotalPrice.Value;
                newHotelBookingDetail.TotalVendorPrice = hotelBookingDetail.TotalVendorPrice.Value;
                newHotelBookingDetail.VendorDiscount = hotelBookingDetail.VendorDiscount;
                newHotelBookingDetail.VendorPrice = hotelBookingDetail.VendorPrice;
                newHotelBookingDetail.CreateTime = dateTime;
                newHotelBookingDetail.UpdateTime = dateTime;

                entities.AddToHotelBookingDetail(newHotelBookingDetail);
            }

            //VendorSettlement vendorSettlement = new VendorSettlement();

            //if (hotelBooking.PaymentType != "现金")
            //{
            //    if (hotelBooking.BankAccount != null)
            //    {
            //        BankAccount bankAccount = (from b in entities.BankAccount
            //                                   where b.ID == hotelBooking.BankAccount.ID
            //                                   select b).SingleOrDefault();
            //        decimal 余额 = bankAccount.Amount - hotelBooking.TotalVendorPrice.Value;
            //        if (余额 < 0 && !bankAccount.CanNegative)
            //            throw new BusinessException(BusinessFail.余额不足);

            //        bankAccount.Amount = 余额;

            //        vendorSettlement.BankAccountID = hotelBooking.BankAccount.ID;
            //    }
            //}


            //vendorSettlement.ID = Guid.NewGuid();
            //vendorSettlement.PayType = hotelBooking.CustomerPaymentType;
            //vendorSettlement.PostingDate = hotelBooking.OrderDate.Value;
            //vendorSettlement.Ref = newHotelBooking.Ref;
            //vendorSettlement.Remark = string.Empty;
            //vendorSettlement.Amount = hotelBooking.TotalVendorPrice.Value;
            //vendorSettlement.ExchangeRate = hotelBooking.ExchangeRate;
            //vendorSettlement.CurrencyID = hotelBooking.VendorCurrency.ID;
            //vendorSettlement.CheckNo = string.Empty;
            //vendorSettlement.SourceID = hotelBooking.ID;
            //vendorSettlement.Type = hotelBooking.OrderType;
            //vendorSettlement.VendorID = hotelBooking.Vendor.ID;

            //entities.AddToVendorSettlement(vendorSettlement);

            entities.SaveChanges();

            return newHotelBooking.Ref;
        }

        private void checkHotelBooking(Contract.HotelBooking hotelBooking)
        {
            //hotelBooking.ConatctPerson = hotelBooking.ConatctPerson.Trim();
            //hotelBooking.ConatctPersonTel = hotelBooking.ConatctPersonTel.Trim();
            hotelBooking.CustomerPaymentType = hotelBooking.CustomerPaymentType.Trim();
            hotelBooking.Remark = hotelBooking.Remark.Trim();

            //if (hotelBooking.ConatctPerson == string.Empty)
            //    throw new BusinessException(VerificationText.请输入联系人);
            //if (hotelBooking.ConatctPersonTel == string.Empty)
            //    throw new BusinessException(VerificationText.请输入联系人电话);
            if (hotelBooking.CustomerPaymentType == string.Empty)
                throw new BusinessException(VerificationText.请输入客户结算方式);
            if (hotelBooking.Customer == null)
                throw new BusinessException(VerificationText.请输入客户);
            if (hotelBooking.Vendor == null)
                throw new BusinessException(VerificationText.请输入供应商);
            if (hotelBooking.VendorCurrency == null)
                throw new BusinessException(VerificationText.请输入货币);
            if (hotelBooking.OrderDate == null || hotelBooking.OrderDate.Value == DateTime.MinValue)
                throw new BusinessException(VerificationText.请输入开单日期);
            if (hotelBooking.OrderHotelUser == null)
                throw new BusinessException(VerificationText.请输入订房人);
            if (hotelBooking.OrderUser == null)
                throw new BusinessException(VerificationText.请输入经办人);
            if (hotelBooking.Details.Count == 0)
                throw new BusinessException(VerificationText.请输入酒店订单明细);

            foreach (Contract.HotelBookingDetail hotelBookingDetail in hotelBooking.Details)
            {
                checkHotelBookingDetail(hotelBookingDetail);
            }
        }

        private void checkHotelBookingDetail(Contract.HotelBookingDetail hotelBookingDetail)
        {
            hotelBookingDetail.GuestName = hotelBookingDetail.GuestName.Trim();
            hotelBookingDetail.HotelConfirmationRef = hotelBookingDetail.HotelConfirmationRef.Trim();
            hotelBookingDetail.HotelName = hotelBookingDetail.HotelName.Trim();
            hotelBookingDetail.HotelOrderRef = hotelBookingDetail.HotelOrderRef.Trim();

            if (hotelBookingDetail.CheckInDate == null)
                throw new BusinessException(VerificationText.请输入入住日期);
            if (hotelBookingDetail.CheckOutDate == null)
                throw new BusinessException(VerificationText.请输入离店日期);
            if (hotelBookingDetail.GuestName == string.Empty)
                throw new BusinessException(VerificationText.请输入顾客名称);
            //if (hotelBookingDetail.HotelConfirmationRef == string.Empty)
            //    throw new BusinessException(VerificationText.请输入酒店确认号);
            //if (hotelBookingDetail.HotelOrderRef == string.Empty)
            //    throw new BusinessException(VerificationText.请输入酒店订单号);
            if (hotelBookingDetail.Rooms == 0)
                throw new BusinessException(VerificationText.请输入间数);
            if (hotelBookingDetail.TotalVendorPrice == 0)
                throw new BusinessException(VerificationText.底价返点酒店扣税输入不正确);
        }

        public string UpdateHotelBooking(Contract.HotelBooking hotelBooking)
        {
            foreach (Contract.HotelBookingDetail hotelBookingDetail in hotelBooking.Details)
                hotelBookingDetail.HotelBooking = hotelBooking;

            checkHotelBooking(hotelBooking);

            DateTime dateTime = DateTime.Now;


            HotelBooking dataHotelBooking = (from hb in entities.HotelBooking
                                             where hb.ID == hotelBooking.ID
                                             select hb).SingleOrDefault();
            if (dataHotelBooking == null)
            {
                return NewHotelBooking(hotelBooking);
            }

            //dataHotelBooking.ContactPerson = hotelBooking.ConatctPerson;
            //dataHotelBooking.ContactPersonTel = hotelBooking.ConatctPersonTel;
            dataHotelBooking.Address = hotelBooking.Address.Trim();
            dataHotelBooking.CurrencyID = hotelBooking.Currency.ID;
            dataHotelBooking.CustomerID = hotelBooking.Customer.ID;
            dataHotelBooking.Intergral = hotelBooking.TotalIntergral.Value;
            dataHotelBooking.OrderDate = hotelBooking.OrderDate.Value;
            dataHotelBooking.OrderHotelUserID = hotelBooking.OrderHotelUser.ID;
            dataHotelBooking.OrderUserID = hotelBooking.OrderUser.ID;
            dataHotelBooking.PaymentType = hotelBooking.CustomerPaymentType;
            dataHotelBooking.PrintTotalPrice = hotelBooking.PrintTotalPrice;
            dataHotelBooking.Remark = hotelBooking.Remark;
            dataHotelBooking.RoomTotalQty = hotelBooking.RoomTotalQty;
            dataHotelBooking.SalesmanID = hotelBooking.Salesman.ID;
            dataHotelBooking.Status = hotelBooking.Status;
            dataHotelBooking.TotalPrice = hotelBooking.TotalPrice.Value;
            dataHotelBooking.TotalVendorPrice = hotelBooking.TotalVendorPrice.Value;
            dataHotelBooking.ExchangeRate = hotelBooking.ExchangeRate;
            dataHotelBooking.TotalProfit = hotelBooking.TotalProfit.Value;
            dataHotelBooking.DeliveryDate = hotelBooking.DeliveryDate;
            dataHotelBooking.ProvideTicketDate = hotelBooking.ProvideTicketDate;
            dataHotelBooking.ProvideTicketUserID = hotelBooking.ProvideTicketUser.ID;
            dataHotelBooking.VendorID = hotelBooking.Vendor.ID;
            dataHotelBooking.VendorCurrencyID = hotelBooking.VendorCurrency.ID;
            dataHotelBooking.VoidCause = null;
            dataHotelBooking.UpdateTime = dateTime;

            foreach (Contract.HotelBookingDetail hotelBookingDetail in hotelBooking.Details)
            {
                HotelBookingDetail dataHotelBookingDetail = (from hbd in entities.HotelBookingDetail
                                                             where hbd.ID == hotelBookingDetail.ID
                                                             select hbd).SingleOrDefault();

                if (dataHotelBookingDetail == null)
                {
                    dataHotelBookingDetail = new HotelBookingDetail();
                    dataHotelBookingDetail.ID = hotelBookingDetail.ID;
                    dataHotelBookingDetail.CheckInDate = hotelBookingDetail.CheckInDate.Value;
                    dataHotelBookingDetail.CheckOutDate = hotelBookingDetail.CheckOutDate.Value;
                    dataHotelBookingDetail.Days = hotelBookingDetail.Days;
                    dataHotelBookingDetail.GuestName = hotelBookingDetail.GuestName;
                    dataHotelBookingDetail.HotelBookingID = hotelBooking.ID;
                    dataHotelBookingDetail.HotelConfirmationRef = hotelBookingDetail.HotelConfirmationRef;
                    dataHotelBookingDetail.HotelLevel = hotelBookingDetail.HotelLeave;
                    dataHotelBookingDetail.HotelName = hotelBookingDetail.HotelName;
                    dataHotelBookingDetail.HotelOrderRef = hotelBookingDetail.HotelOrderRef;
                    dataHotelBookingDetail.HotelTax = hotelBookingDetail.HotelTax;
                    dataHotelBookingDetail.Intergral = hotelBookingDetail.Intergral;
                    dataHotelBookingDetail.Price = hotelBookingDetail.Price;
                    dataHotelBookingDetail.Profit = hotelBookingDetail.Profit.Value;
                    dataHotelBookingDetail.Rooms = hotelBookingDetail.Rooms;
                    dataHotelBookingDetail.RoomType = hotelBookingDetail.RoomType;
                    dataHotelBookingDetail.TotalPrice = hotelBookingDetail.TotalPrice.Value;
                    dataHotelBookingDetail.TotalVendorPrice = hotelBookingDetail.TotalVendorPrice.Value;
                    dataHotelBookingDetail.VendorDiscount = hotelBookingDetail.VendorDiscount;
                    dataHotelBookingDetail.VendorPrice = hotelBookingDetail.VendorPrice;
                    dataHotelBookingDetail.CreateTime = dateTime;
                    dataHotelBookingDetail.UpdateTime = dateTime;

                    entities.AddToHotelBookingDetail(dataHotelBookingDetail);
                }
                else
                {
                    dataHotelBookingDetail.CheckInDate = hotelBookingDetail.CheckInDate.Value;
                    dataHotelBookingDetail.CheckOutDate = hotelBookingDetail.CheckOutDate.Value;
                    dataHotelBookingDetail.Days = hotelBookingDetail.Days;
                    dataHotelBookingDetail.GuestName = hotelBookingDetail.GuestName;
                    dataHotelBookingDetail.HotelBookingID = hotelBooking.ID;
                    dataHotelBookingDetail.HotelConfirmationRef = hotelBookingDetail.HotelConfirmationRef;
                    dataHotelBookingDetail.HotelLevel = hotelBookingDetail.HotelLeave;
                    dataHotelBookingDetail.HotelName = hotelBookingDetail.HotelName;
                    dataHotelBookingDetail.HotelOrderRef = hotelBookingDetail.HotelOrderRef;
                    dataHotelBookingDetail.HotelTax = hotelBookingDetail.HotelTax;
                    dataHotelBookingDetail.Intergral = hotelBookingDetail.Intergral;
                    dataHotelBookingDetail.Price = hotelBookingDetail.Price;
                    dataHotelBookingDetail.Profit = hotelBookingDetail.Profit.Value;
                    dataHotelBookingDetail.Rooms = hotelBookingDetail.Rooms;
                    dataHotelBookingDetail.RoomType = hotelBookingDetail.RoomType;
                    dataHotelBookingDetail.TotalPrice = hotelBookingDetail.TotalPrice.Value;
                    dataHotelBookingDetail.TotalVendorPrice = hotelBookingDetail.TotalVendorPrice.Value;
                    dataHotelBookingDetail.VendorDiscount = hotelBookingDetail.VendorDiscount;
                    dataHotelBookingDetail.VendorPrice = hotelBookingDetail.VendorPrice;
                    dataHotelBookingDetail.UpdateTime = dateTime;
                }
            }

            entities.SaveChanges();

            return dataHotelBooking.Ref;
        }

        public void VoidHotelBooking(Contract.HotelBooking hotelBooking, string cause)
        {
            cause = cause.Trim();

            DateTime dateTime = DateTime.Now;


            HotelBooking dataHotelBooking = (from hb in entities.HotelBooking
                                             where hb.ID == hotelBooking.ID
                                             select hb).SingleOrDefault();
            if (dataHotelBooking == null)
                throw new BusinessException(BusinessFail.酒店订单不存在);
            if (dataHotelBooking.Status.Trim() == "已作废")
                throw new BusinessException(BusinessFail.酒店订单已作废);
            if (dataHotelBooking.CustomerStatus.Trim() == "已付款")
                throw new BusinessException(BusinessFail.客户已付款);

            dataHotelBooking.Status = "已作废";
            dataHotelBooking.VoidCause = cause;
            dataHotelBooking.UpdateTime = dateTime;


            VendorSettlement vendorSettlement = (from vs in entities.VendorSettlement
                                                 where vs.SourceID == hotelBooking.ID
                                                 select vs).SingleOrDefault();
            if (vendorSettlement != null)
            {
                if (vendorSettlement.PayType != "现金")
                {
                    if (vendorSettlement.BankAccountID != null)
                    {
                        BankAccount bankAccount = (from b in entities.BankAccount
                                                   where b.ID == vendorSettlement.BankAccount.ID
                                                   select b).SingleOrDefault();

                        bankAccount.Amount += vendorSettlement.Amount;
                    }
                }

                entities.DeleteObject(vendorSettlement);
            }
            entities.SaveChanges();
        }

        public string NewOtherBusiness(Contract.OtherBusiness otherBusiness)
        {
            checkOtherBusiness(otherBusiness);
            DateTime dateTime = DateTime.Now;


            OtherBusiness newOtherBusiness = new OtherBusiness();
            newOtherBusiness.ID = otherBusiness.ID;
            newOtherBusiness.Ref = new BaseBL().GetNewNr("Other");
            newOtherBusiness.Address = otherBusiness.Address;
            newOtherBusiness.BasePrice = otherBusiness.BasePrice;
            newOtherBusiness.ContactPerson = otherBusiness.ContactPerson;
            newOtherBusiness.ContactPersonTel = otherBusiness.ContactPersonTel;
            newOtherBusiness.Contenct = otherBusiness.Content;
            newOtherBusiness.CreditTerm = otherBusiness.CreditTerm;
            newOtherBusiness.CurrencyID = otherBusiness.Currency.ID;
            newOtherBusiness.CustomerID = otherBusiness.Customer.ID;
            newOtherBusiness.CustomerName = otherBusiness.CustomerName;
            newOtherBusiness.Intergral = otherBusiness.Intergral;
            newOtherBusiness.OrderDate = otherBusiness.OrderDate.Value;
            newOtherBusiness.OrderUserID = otherBusiness.OrderUser.ID;
            newOtherBusiness.ProvideTicketUserID = otherBusiness.ProvideTicketUser.ID;
            newOtherBusiness.Price = otherBusiness.Price;
            newOtherBusiness.PrintTotalPrice = otherBusiness.PrintTotalPrice;
            newOtherBusiness.ProjectDate = otherBusiness.ProjectDate;
            newOtherBusiness.ProjectID = otherBusiness.Project.ID;
            if (otherBusiness.SubProject != null)
                newOtherBusiness.SubProjectID = otherBusiness.SubProject.ID;
            newOtherBusiness.Qty = otherBusiness.Qty;
            newOtherBusiness.Remark = otherBusiness.Remark;
            newOtherBusiness.SalesmanID = otherBusiness.Salesman.ID;
            newOtherBusiness.DeliveryDate = otherBusiness.DeliveryDate;
            newOtherBusiness.Status = otherBusiness.Status;
            newOtherBusiness.TotalPrice = otherBusiness.TotalPrice;
            newOtherBusiness.TotalVendorPrice = otherBusiness.TotalVendorPrice.Value;
            newOtherBusiness.VendorDiscount = otherBusiness.VendorDiscount;
            newOtherBusiness.TotalProfit = otherBusiness.TotalProfit.Value;
            newOtherBusiness.VendorID = otherBusiness.Vendor.ID;
            newOtherBusiness.VoidCause = null;
            newOtherBusiness.CustomerStatus = otherBusiness.CustomerStatus;
            newOtherBusiness.VendorStatus = "未付款";
            newOtherBusiness.CreateTime = dateTime;
            newOtherBusiness.UpdateTime = dateTime;

            entities.AddToOtherBusiness(newOtherBusiness);

            //VendorSettlement vendorSettlement = new VendorSettlement();

            //if (otherBusiness.PaymentType != "现金")
            //{
            //    if (otherBusiness.BankAccount != null)
            //    {
            //        BankAccount bankAccount = (from b in entities.BankAccount
            //                                   where b.ID == otherBusiness.BankAccount.ID
            //                                   select b).SingleOrDefault();
            //        decimal 余额 = bankAccount.Amount - otherBusiness.TotalVendorPrice.Value;
            //        if (余额 < 0 && !bankAccount.CanNegative)
            //            throw new BusinessException(BusinessFail.余额不足);

            //        bankAccount.Amount = 余额;

            //        vendorSettlement.BankAccountID = otherBusiness.BankAccount.ID;
            //    }
            //}

            //vendorSettlement.ID = Guid.NewGuid();
            //vendorSettlement.PayType = otherBusiness.PaymentType;
            //vendorSettlement.PostingDate = otherBusiness.OrderDate.Value;
            //vendorSettlement.Ref = newOtherBusiness.Ref;
            //vendorSettlement.Remark = string.Empty;
            //vendorSettlement.Amount = otherBusiness.TotalVendorPrice.Value;
            //vendorSettlement.ExchangeRate = 1;
            //vendorSettlement.CurrencyID = otherBusiness.Currency.ID;
            //vendorSettlement.CheckNo = string.Empty;
            //vendorSettlement.SourceID = otherBusiness.ID;
            //vendorSettlement.Type = otherBusiness.OrderType;
            //vendorSettlement.VendorID = otherBusiness.Vendor.ID;

            //entities.AddToVendorSettlement(vendorSettlement);


            entities.SaveChanges();

            return newOtherBusiness.Ref;
        }

        private void checkOtherBusiness(Contract.OtherBusiness otherBusiness)
        {
            //otherBusiness.Address = otherBusiness.Address.Trim();
            //otherBusiness.ContactPerson = otherBusiness.ContactPerson.Trim();
            //otherBusiness.ContactPersonTel = otherBusiness.ContactPersonTel.Trim();
            otherBusiness.Content = otherBusiness.Content.Trim();
            otherBusiness.CreditTerm = otherBusiness.CreditTerm.Trim();
            otherBusiness.CustomerName = otherBusiness.CustomerName.Trim();
            otherBusiness.Remark = otherBusiness.Remark.Trim();

            //if (otherBusiness.Address == string.Empty)
            //    throw new BusinessException(VerificationText.请输入地址);
            //if (otherBusiness.ContactPerson == string.Empty)
            //    throw new BusinessException(VerificationText.请输入联系人);
            //if (otherBusiness.ContactPersonTel == string.Empty)
            //    throw new BusinessException(VerificationText.请输入联系人电话);
            if (otherBusiness.CreditTerm == string.Empty)
                throw new BusinessException(VerificationText.请输入客户结算方式);
            if (otherBusiness.Customer == null)
                throw new BusinessException(VerificationText.请输入客户);
            if (otherBusiness.OrderDate == null || otherBusiness.OrderDate.Value == DateTime.MinValue)
                throw new BusinessException(VerificationText.请输入开单日期);
            if (otherBusiness.OrderUser == null)
                throw new BusinessException(VerificationText.请输入订票人);
            if (otherBusiness.Project == null)
                throw new BusinessException(VerificationText.请输入项目);
            if (otherBusiness.Content == string.Empty)
                throw new BusinessException(VerificationText.请输入项目内容);
            if (otherBusiness.Vendor == null)
                throw new BusinessException(VerificationText.请输入供应商);
            if (otherBusiness.Qty == 0)
                throw new BusinessException(VerificationText.请输入数量);
            if (otherBusiness.ProjectDate == DateTime.MinValue)
                throw new BusinessException(VerificationText.请输入项目日期);
            if (otherBusiness.TotalVendorPrice == null)
                throw new BusinessException(VerificationText.请输入净价或者返点);

        }

        public string UpdateOtherBusiness(Contract.OtherBusiness otherBusiness)
        {
            checkOtherBusiness(otherBusiness);

            DateTime dateTime = DateTime.Now;


            OtherBusiness oldOtherBusiness = (from ob in entities.OtherBusiness
                                              where ob.ID == otherBusiness.ID
                                              select ob).SingleOrDefault();
            if (oldOtherBusiness == null)
            {
                return NewOtherBusiness(otherBusiness);
            }

            oldOtherBusiness.Address = otherBusiness.Address;
            oldOtherBusiness.BasePrice = otherBusiness.BasePrice;
            //oldOtherBusiness.ContactPerson = otherBusiness.ContactPerson;
            //oldOtherBusiness.ContactPersonTel = otherBusiness.ContactPersonTel;
            oldOtherBusiness.Contenct = otherBusiness.Content;
            oldOtherBusiness.CreditTerm = otherBusiness.CreditTerm;
            oldOtherBusiness.CurrencyID = otherBusiness.Currency.ID;
            oldOtherBusiness.CustomerID = otherBusiness.Customer.ID;
            oldOtherBusiness.CustomerName = otherBusiness.CustomerName;
            oldOtherBusiness.Intergral = otherBusiness.Intergral;
            oldOtherBusiness.OrderDate = otherBusiness.OrderDate.Value;
            oldOtherBusiness.OrderUserID = otherBusiness.OrderUser.ID;
            oldOtherBusiness.ProvideTicketUserID = otherBusiness.ProvideTicketUser.ID;
            oldOtherBusiness.Price = otherBusiness.Price;
            oldOtherBusiness.PrintTotalPrice = otherBusiness.PrintTotalPrice;
            oldOtherBusiness.ProjectDate = otherBusiness.ProjectDate;
            oldOtherBusiness.ProjectID = otherBusiness.Project.ID;
            if (otherBusiness.SubProject != null)
                oldOtherBusiness.SubProjectID = otherBusiness.SubProject.ID;
            oldOtherBusiness.Qty = otherBusiness.Qty;
            oldOtherBusiness.Remark = otherBusiness.Remark;
            oldOtherBusiness.SalesmanID = otherBusiness.Salesman.ID;
            oldOtherBusiness.DeliveryDate = otherBusiness.DeliveryDate;
            oldOtherBusiness.Status = otherBusiness.Status;
            oldOtherBusiness.TotalPrice = otherBusiness.TotalPrice;
            oldOtherBusiness.TotalVendorPrice = otherBusiness.TotalVendorPrice.Value;
            oldOtherBusiness.TotalProfit = otherBusiness.TotalProfit.Value;
            oldOtherBusiness.VendorDiscount = otherBusiness.VendorDiscount;
            oldOtherBusiness.VendorID = otherBusiness.Vendor.ID;
            oldOtherBusiness.VoidCause = null;
            oldOtherBusiness.UpdateTime = dateTime;

            entities.SaveChanges();

            return oldOtherBusiness.Ref;
        }

        public void VoidOtherBusiness(Contract.OtherBusiness otherBusiness, string cause)
        {
            cause = cause.Trim();

            DateTime dateTime = DateTime.Now;


            OtherBusiness oldOtherBusiness = (from ob in entities.OtherBusiness
                                              where ob.ID == otherBusiness.ID
                                              select ob).SingleOrDefault();
            if (oldOtherBusiness == null)
                throw new BusinessException(BusinessFail.其他项目订单不存在);
            if (oldOtherBusiness.Status.Trim() == "已作废")
                throw new BusinessException(BusinessFail.其他项目订单已作废);
            if (oldOtherBusiness.CustomerStatus.Trim() == "已付款")
                throw new BusinessException(BusinessFail.客户已付款);

            oldOtherBusiness.VoidCause = cause;
            oldOtherBusiness.Status = "已作废";
            oldOtherBusiness.UpdateTime = dateTime;


            VendorSettlement vendorSettlement = (from vs in entities.VendorSettlement
                                                 where vs.SourceID == otherBusiness.ID
                                                 select vs).SingleOrDefault();
            if (vendorSettlement != null)
            {
                if (vendorSettlement.PayType != "现金")
                {
                    if (vendorSettlement.BankAccountID != null)
                    {
                        BankAccount bankAccount = (from b in entities.BankAccount
                                                   where b.ID == vendorSettlement.BankAccount.ID
                                                   select b).SingleOrDefault();

                        bankAccount.Amount += vendorSettlement.Amount;
                    }
                }

                entities.DeleteObject(vendorSettlement);
            }
            entities.SaveChanges();
        }

        public List<Contract.IntergralExchange> SearchIntergralExchange(DateTime month) 
        {
            DateTime startDateTime = new DateTime(month.Year, month.Month, 1);
            DateTime endDateTime = startDateTime.AddMonths(1).AddMilliseconds(-5);

            return (from ie in entities.IntergralExchange
                    where ie.UpdateTime >= startDateTime &&
                          ie.UpdateTime <= endDateTime
                    orderby ie.UpdateTime
                    select new Contract.IntergralExchange()
                    {
                        ID = ie.ID,
                        Amount = ie.Amount,
                        CustomerText = ie.Customer.Name.Trim(),
                        Date = ie.Date,
                        Item = ie.Item.Trim(),
                        Qty = ie.Qty
                    }).ToList();
        }

        public void IntergralExchange(Contract.IntergralExchange intergralExchange)
        {
            DateTime dateTime = DateTime.Now;


            IntergralExchange newIntergralExchange = new IntergralExchange();
            newIntergralExchange.ID = intergralExchange.ID;
            newIntergralExchange.Amount = intergralExchange.Amount;
            newIntergralExchange.CustomerID = intergralExchange.Customer.ID;
            newIntergralExchange.Date = intergralExchange.Date;
            newIntergralExchange.Item = intergralExchange.Item;
            newIntergralExchange.Qty = intergralExchange.Qty;
            newIntergralExchange.Receiptor = intergralExchange.Receiptor;
            newIntergralExchange.Remark = intergralExchange.Remark;
            newIntergralExchange.CreateTime = dateTime;
            newIntergralExchange.UpdateTime = dateTime;

            Customer customer = (from c in entities.Customer
                                 where c.ID == newIntergralExchange.CustomerID
                                 select c).SingleOrDefault();

            customer.Intergral -= newIntergralExchange.Amount;

            entities.AddToIntergralExchange(newIntergralExchange);
            entities.SaveChanges();
        }

        #endregion

        #region Load Data

        public List<Contract.RFlightOrder> GetAllFlightOrder()
        {
            return (from fo in entities.FlightOrder
                    orderby fo.Ref descending 
                    select new Contract.RFlightOrder()
                    {
                        ID = fo.ID,
                        Ref = fo.Ref.Trim(),
                        OrderDate = fo.OrderDate,
                        TotalPrice = fo.TotalAccountReceivable,
                        //TotalAccountReceivable = fo.TotalAccountReceivable,
                        Status = fo.Status.Trim(),
                        CustomerStatus = fo.CustomerStatus.Trim(),
                        Customer = (from c in entities.Customer
                                    where c.ID == fo.CustomerID
                                    select c.Name.Trim()).FirstOrDefault(),
                    }).ToList();
        }

        public List<Contract.RFlightOrder> GetFlightOrderByRef(string _ref)
        {
            _ref = _ref.Trim();
            if (_ref == string.Empty)
                throw new BusinessException(VerificationText.请输入订单号);


            return (from fo in entities.FlightOrder
                    where fo.Ref.IndexOf(_ref.Trim()) >= 0
                    orderby fo.Ref descending 
                    select new Contract.RFlightOrder()
                    {
                        ID = fo.ID,
                        Ref = fo.Ref.Trim(),
                        OrderDate = fo.OrderDate,
                        TotalPrice = fo.TotalAccountReceivable,
                        //TotalAccountReceivable = fo.TotalAccountReceivable,
                        Status = fo.Status.Trim(),
                        CustomerStatus = fo.CustomerStatus.Trim(),
                        Customer = (from c in entities.Customer
                                    where c.ID == fo.CustomerID
                                    select c.Name.Trim()).FirstOrDefault(),
                    }).ToList();
        }

        public List<Contract.RFlightOrder> GetFlightOrderByCustomer(Contract.RCustomer customer)
        {
            if (customer == null)
                throw new BusinessException(VerificationText.请输入客户);


            return (from fo in entities.FlightOrder
                    where fo.CustomerID == customer.ID
                    orderby fo.Ref descending
                    select new Contract.RFlightOrder()
                    {
                        ID = fo.ID,
                        Ref = fo.Ref.Trim(),
                        OrderDate = fo.OrderDate,
                        TotalPrice = fo.TotalAccountReceivable,
                        //TotalAccountReceivable = fo.TotalAccountReceivable,
                        Status = fo.Status.Trim(),
                        CustomerStatus = fo.CustomerStatus.Trim(),
                        Customer = customer.Name
                    }).ToList();
        }

        public List<Contract.RFlightOrder> GetFlightOrderByOrderDate(DateTime startDate, DateTime endDate)
        {

            return (from fo in entities.FlightOrder
                    where fo.OrderDate >= startDate && fo.OrderDate <= endDate
                    orderby fo.Ref descending
                    select new Contract.RFlightOrder()
                    {
                        ID = fo.ID,
                        Ref = fo.Ref.Trim(),
                        OrderDate = fo.OrderDate,
                        TotalPrice = fo.TotalAccountReceivable,
                        //TotalAccountReceivable = fo.TotalAccountReceivable,
                        Status = fo.Status.Trim(),
                        CustomerStatus = fo.CustomerStatus.Trim(),
                        Customer = (from c in entities.Customer
                                    where c.ID == fo.CustomerID
                                    select c.Name.Trim()).FirstOrDefault(),
                    }).ToList();
        }

        public List<Contract.RFlightOrder> GetFlightOrderByVendor(Contract.RVendor vendor)
        {
            if (vendor == null)
                throw new BusinessException(VerificationText.请输入供应商);


            return (from fo in entities.FlightOrder
                    where (from fod in entities.FlightOrderDetail
                           where fod.FlightOrderID == fo.ID &&
                                 fod.VendorID == vendor.ID
                           group fod by fod.FlightOrderID into g
                           select g.Key).Any(fodid => fo.ID == fodid)
                    orderby fo.Ref descending
                    select new Contract.RFlightOrder()
                    {
                        ID = fo.ID,
                        Ref = fo.Ref.Trim(),
                        OrderDate = fo.OrderDate,
                        TotalPrice = fo.TotalAccountReceivable,
                        //TotalAccountReceivable = fo.TotalAccountReceivable,
                        Status = fo.Status.Trim(),
                        CustomerStatus = fo.CustomerStatus.Trim(),
                        Customer = (from c in entities.Customer
                                    where c.ID == fo.CustomerID
                                    select c.Name.Trim()).FirstOrDefault(),
                    }).ToList();
        }

        public List<Contract.RFlightOrder> GetFlightOrderByPassenger(string passenger)
        {
            passenger = passenger.Trim();
            if (passenger == string.Empty)
                throw new BusinessException(VerificationText.请输入乘客);

            return (from fo in entities.FlightOrder
                    where (from fod in entities.FlightOrderDetail
                           where fod.FlightOrderID == fo.ID &&
                                 (from p in entities.Passenger
                                  where p.CName.IndexOf(passenger.Trim()) >= 0 ||
                                        p.EName.IndexOf(passenger.Trim()) >= 0
                                  select p.ID).Any(pid => fod.PassengerID == pid)
                           group fod by fod.FlightOrderID into g
                           select g.Key).Any(fodid => fo.ID == fodid)
                    orderby fo.Ref descending
                    select new Contract.RFlightOrder()
                    {
                        ID = fo.ID,
                        Ref = fo.Ref.Trim(),
                        OrderDate = fo.OrderDate,
                        TotalPrice = fo.TotalAccountReceivable,
                        //TotalAccountReceivable = fo.TotalAccountReceivable,
                        Status = fo.Status.Trim(),
                        CustomerStatus = fo.CustomerStatus.Trim(),
                        Customer = (from c in entities.Customer
                                    where c.ID == fo.CustomerID
                                    select c.Name.Trim()).FirstOrDefault(),
                    }).ToList();
        }

        public List<Contract.RFlightOrder> GetFlightOrderByIsKeep(bool isKeep)
        {

            return (from fo in entities.FlightOrder
                    where (from fod in entities.FlightOrderDetail
                           where fod.FlightOrderID == fo.ID &&
                                 (fod.Status.Trim() == "留用") == isKeep
                           group fod by fod.FlightOrderID into g
                           select g.Key).Any(fodid => fo.ID == fodid)
                    orderby fo.Ref descending
                    select new Contract.RFlightOrder()
                    {
                        ID = fo.ID,
                        Ref = fo.Ref.Trim(),
                        OrderDate = fo.OrderDate,
                        TotalPrice = fo.TotalAccountReceivable,
                        //TotalAccountReceivable = fo.TotalAccountReceivable,
                        Status = fo.Status.Trim(),
                        CustomerStatus = fo.CustomerStatus.Trim(),
                        Customer = (from c in entities.Customer
                                    where c.ID == fo.CustomerID
                                    select c.Name.Trim()).FirstOrDefault(),
                    }).ToList();
        }

        public List<Contract.RFlightOrder> GetFlightOrderByTicketNo(string ticketNo)
        {
            ticketNo = ticketNo.Trim();
            if (ticketNo == string.Empty)
                throw new BusinessException(VerificationText.请输入机票号);


            return (from fo in entities.FlightOrder
                    where (from fod in entities.FlightOrderDetail
                           where fod.FlightOrderID == fo.ID &&
                                 fod.TicketNo.IndexOf(ticketNo.Trim()) >= 0
                           group fod by fod.FlightOrderID into g
                           select g.Key).Any(fodid => fo.ID == fodid)
                    orderby fo.Ref descending
                    select new Contract.RFlightOrder()
                    {
                        ID = fo.ID,
                        Ref = fo.Ref.Trim(),
                        OrderDate = fo.OrderDate,
                        TotalPrice = fo.TotalAccountReceivable,
                        //TotalAccountReceivable = fo.TotalAccountReceivable,
                        Status = fo.Status.Trim(),
                        CustomerStatus = fo.CustomerStatus.Trim(),
                        Customer = (from c in entities.Customer
                                    where c.ID == fo.CustomerID
                                    select c.Name.Trim()).FirstOrDefault(),
                    }).ToList();
        }

        public List<Contract.RFlightOrder> GetFlightOrderByAirwaysOrderNo(string airwaysOrderNo)
        {
            airwaysOrderNo = airwaysOrderNo.Trim();
            if (airwaysOrderNo == string.Empty)
                throw new BusinessException(VerificationText.请输入航空公司订单号);


            return (from fo in entities.FlightOrder
                    where (from fod in entities.FlightOrderDetail
                           where fod.FlightOrderID == fo.ID &&
                                 fod.AirwaysOrderNo.IndexOf(airwaysOrderNo.Trim()) >= 0
                           group fod by fod.FlightOrderID into g
                           select g.Key).Any(fodid => fo.ID == fodid)
                    orderby fo.Ref descending
                    select new Contract.RFlightOrder()
                    {
                        ID = fo.ID,
                        Ref = fo.Ref.Trim(),
                        OrderDate = fo.OrderDate,
                        TotalPrice = fo.TotalAccountReceivable,
                        //TotalAccountReceivable = fo.TotalAccountReceivable,
                        Status = fo.Status.Trim(),
                        CustomerStatus = fo.CustomerStatus.Trim(),
                        Customer = (from c in entities.Customer
                                    where c.ID == fo.CustomerID
                                    select c.Name.Trim()).FirstOrDefault(),
                    }).ToList();
        }

        public List<Contract.RFlightOrder> GetFlightOrderByWayType(string wayType)
        {

            return (from fo in entities.FlightOrder
                    where (from fod in entities.FlightOrderDetail
                           where fod.FlightOrderID == fo.ID &&
                                 fod.WayType.IndexOf(wayType.Trim()) >= 0
                           group fod by fod.FlightOrderID into g
                           select g.Key).Any(fodid => fo.ID == fodid)
                    orderby fo.Ref descending
                    select new Contract.RFlightOrder()
                    {
                        ID = fo.ID,
                        Ref = fo.Ref.Trim(),
                        OrderDate = fo.OrderDate,
                        TotalPrice = fo.TotalAccountReceivable,
                        //TotalAccountReceivable = fo.TotalAccountReceivable,
                        Status = fo.Status.Trim(),
                        CustomerStatus = fo.CustomerStatus.Trim(),
                        Customer = (from c in entities.Customer
                                    where c.ID == fo.CustomerID
                                    select c.Name.Trim()).FirstOrDefault(),
                    }).ToList();
        }

        public List<Contract.RFlightOrder> GetFlightOrderByJourneyOrder(string journeyOrder)
        {
            return (from fo in entities.FlightOrder
                    where (from fod in entities.FlightOrderDetail
                           where fod.FlightOrderID == fo.ID &&
                                 (from jo in entities.JourneyOrder
                                  where jo.Ref.IndexOf(journeyOrder.Trim()) >= 0
                                  select jo.FlightDetailID).Contains(fod.ID)
                           group fod by fod.FlightOrderID into g
                           select g.Key).Any(fodid => fo.ID == fodid)
                    orderby fo.Ref descending
                    select new Contract.RFlightOrder()
                    {
                        ID = fo.ID,
                        Ref = fo.Ref.Trim(),
                        OrderDate = fo.OrderDate,
                        TotalPrice = fo.TotalAccountReceivable,
                        //TotalAccountReceivable = fo.TotalAccountReceivable,
                        Status = fo.Status.Trim(),
                        CustomerStatus = fo.CustomerStatus.Trim(),
                        Customer = (from c in entities.Customer
                                    where c.ID == fo.CustomerID
                                    select c.Name.Trim()).FirstOrDefault(),
                    }).ToList();
        }

        public Contract.PFlightOrder GetPrintFlightOrder(Guid id)
        {

            Contract.PFlightOrder flightOrder =
                (from fo in entities.FlightOrder
                 where  fo.ID == id
                 select new Contract.PFlightOrder()
                 {
                     ID = fo.ID,
                     客户地址 = fo.Address.Trim(),
                     客户名称 = fo.Customer.Name.Trim(),
                     练习电话 = fo.ContactPersonTel.Trim(),
                     结算方式 = fo.CreditTerm.Trim(),
                     DeliveryDate = fo.DeliveryDate,
                     订单号 = fo.Ref.Trim(),
                     备注 = fo.Remark.Trim(),
                     经办人 = fo.User3.Name.Trim(),
                     订单人 = fo.User1.Name.Trim(),
                     总金额 = fo.TotalAccountReceivable,
                     联系人 = fo.ContactPerson,
                 }).SingleOrDefault();

            flightOrder.Details =
                (from fw in entities.FlightWay 
                 join fod in entities.FlightOrderDetail on fw.FlightOrderDetailID equals fod.ID
                 where fod.FlightOrderID == flightOrder.ID
                 orderby fod.Seq, fw.Seq
                 select new Contract.PFlightOrderDetail()
                 {
                     DInsuranceFee = fod.InsuranceFee,
                     IsInsurancePolicyFree = fod.IsInsurancePolicyFree,
                     PassengerText = fod.Passenger.CName.Trim(),
                     DPrice = fod.Price,
                     Tax = fod.Tax1 + fod.Tax2,
                     DepartLocation = fw.DepartLocation.Trim(),
                     Destination = fw.Destination.Trim(),
                     FlightNo = fw.FlightNo.Trim(),
                     Seq = fw.Seq,
                     出发日期 = fw.DepartDate,
                 }).ToList();

            return flightOrder;
        }

        public Contract.FlightOrder GetFlightOrderByID(Guid id)
        {

            Contract.FlightOrder flightOrder =
                (from fo in entities.FlightOrder
                 where fo.ID == id
                 select new Contract.FlightOrder()
                 {
                     ID = fo.ID,
                     Address = fo.Address.Trim(),
                     ContactPerson = fo.ContactPerson.Trim(),
                     ContactPersonTel = fo.ContactPersonTel.Trim(),
                     CreditTerm = fo.CreditTerm.Trim(),
                     Currency = new Contract.Currency() { ID = fo.CurrencyID },
                     Customer = new Contract.Customer() { ID = fo.CustomerID },
                     //ExchangeRate = fo.ExchageRate,
                     //TotalIntergral = fo.Intergral,
                     IsInternational = fo.IsInternational,
                     OrderDate = fo.OrderDate,
                     OrderTicketUser = new Contract.User() { ID = fo.OrderTicketUserID },
                     OrderUser = new Contract.User() { ID = fo.OrderUserID },
                     //Phone = fo.Phone.Trim(),
                     PrintTotalPrice = fo.PrintTotalPrice,
                     ProvideTicketDate = fo.ProvideTicketDate,
                     ProvideTicketUser = new Contract.User() { ID = fo.ProvideTicketUserID },
                     DeliveryDate = fo.DeliveryDate,
                     Ref = fo.Ref.Trim(),
                     Remark = fo.Remark.Trim(),
                     Salesman = new Contract.User() { ID = fo.SalesmanID },
                     Status = fo.Status.Trim(),
                     //TotalAccountPayable = fo.TotalAccountPayable,
                     //TotalAccountReceivable = fo.TotalAccountReceivable,
                     VoidCause = fo.VoidCause.Trim(),
                 }).SingleOrDefault();

            //if (flightOrder == null)
            //    throw new BusinessException(BusinessFail.飞机票订单不存在);

            flightOrder.Currency = new BaseBL().GetCurrencyByID(flightOrder.Currency.ID);
            flightOrder.Customer = new MasterBL().GetCustomerByID(flightOrder.Customer.ID);
            flightOrder.OrderTicketUser = new BaseBL().GetUserByID(flightOrder.OrderTicketUser.ID);
            flightOrder.OrderUser = new BaseBL().GetUserByID(flightOrder.OrderUser.ID);
            flightOrder.ProvideTicketUser = new BaseBL().GetUserByID(flightOrder.ProvideTicketUser.ID);
            flightOrder.Salesman = new BaseBL().GetUserByID(flightOrder.Salesman.ID);

            flightOrder.Details =
                (from fod in entities.FlightOrderDetail
                 where fod.FlightOrderID == flightOrder.ID
                 orderby fod.Seq
                 select new Contract.FlightOrderDatail()
                 {
                     ID = fod.ID,
                     //AccountPayable = fod.AccountPayable,
                     //AccountReceivable = fod.AccountReceivable,
                     AirwaysOrderNo = fod.AirwaysOrderNo.Trim(),
                     Cost = fod.Cost,
                     InsuranceFee = fod.InsuranceFee,
                     //InsurancePolicyID = fod.InsurancePolicyID,
                     IsInsurancePolicyFree = fod.IsInsurancePolicyFree,
                     //JourneyOrderID = fod.JourneyOrderID,
                     //JourneyOrder = new Contract.JourneyOrder()
                     //{
                     //    ID = fod.JourneyOrderID
                     //},
                     NetPrice = fod.NetPrice,
                     Passenger = new Contract.Passenger()
                     {
                         ID = fod.PassengerID
                     },
                     Price = fod.Price,
                     //Profit = fod.Profit,
                     Remark = fod.Remark.Trim(),
                     Status = fod.Status.Trim(),
                     Tax1 = fod.Tax1,
                     Tax2 = fod.Tax2,
                     TicketNo = fod.TicketNo.Trim(),
                     Intergral = fod.Intergral,
                     VendorDiscount = fod.VendorDiscount,
                     Discount = fod.Discount,
                     WayType = fod.WayType.Trim(),
                     Vendor = new Contract.Vendor() { ID = fod.VendorID },
                     VendorCurrency = new Contract.Currency() { ID = fod.VendorCurrencyID },
                     ExchangeRate = fod.ExchangeRate,
                 }).ToList();

            foreach (Contract.FlightOrderDatail flightOrderDetail in flightOrder.Details)
            {
                flightOrderDetail.InsurancePolicyList = (from ip in entities.InsurancePolicy
                                                         where ip.FlightDetailID == flightOrderDetail.ID
                                                         orderby ip.Ref
                                                         select new Contract.InsurancePolicy()
                                                         {
                                                             ID = ip.ID,
                                                             BasePrice = ip.BasePrice,
                                                             Currency = (from c in entities.Currency
                                                                         where c.ID == ip.CurrencyID
                                                                         select new Contract.Currency()
                                                                         {
                                                                             ID = c.ID,
                                                                             Name = c.Name,
                                                                             Code = c.Code
                                                                         }).FirstOrDefault(),
                                                             ListPrice = ip.ListPrice,
                                                             Ref = ip.Ref.Trim(),
                                                             Type = ip.Type.Trim(),
                                                             Vendor = new Contract.RVendor() { ID = ip.VendorID }
                                                         }).ToList();

                foreach (Contract.InsurancePolicy insurancePolicy in flightOrderDetail.InsurancePolicyList)
                {
                    insurancePolicy.Vendor = (from v in entities.Vendor
                                              where v.ID == insurancePolicy.Vendor.ID
                                              select new Contract.RVendor()
                                              {
                                                  ID = v.ID,
                                                  Nr = v.Nr.Trim(),
                                                  Name = v.Name.Trim()
                                              }).FirstOrDefault();
                }

                flightOrderDetail.JourneyOrderList = (from ip in entities.JourneyOrder
                                                      where ip.FlightDetailID == flightOrderDetail.ID
                                                      orderby ip.Ref
                                                      select new Contract.JourneyOrder()
                                                      {
                                                          ID = ip.ID,
                                                          Ref = ip.Ref.Trim(),
                                                          Type = ip.Type.Trim()
                                                      }).ToList();
                flightOrderDetail.Passenger = (from p in entities.Passenger
                                               where p.ID == flightOrderDetail.Passenger.ID
                                               select new Contract.Passenger()
                                               {
                                                   ID = p.ID,
                                                   BirthDay = p.BirthDay,
                                                   CName = p.CName.Trim(),
                                                   Effective = p.Effective,
                                                   EName = p.EName.Trim(),
                                                   FlightCard1 = p.FlightCard1.Trim(),
                                                   FlightCard2 = p.FlightCard2.Trim(),
                                                   FlightCard3 = p.FlightCard3.Trim(),
                                                   IDCardNr1 = p.IDCardNr1.Trim(),
                                                   IDCardNr2 = p.IDCardNr2.Trim(),
                                                   IDCardType1 = p.IDCardType1.Trim(),
                                                   IDCardType2 = p.IDCardType2.Trim(),
                                                   Nation = p.Nation.Trim(),
                                                   Sex = p.Sex.Trim(),
                                                   Tel1 = p.Tel1.Trim(),
                                                   Tel2 = p.Tel2.Trim()
                                               }).FirstOrDefault();
                flightOrderDetail.FlightWayList = (from fw in entities.FlightWay
                                                   where fw.FlightOrderDetailID == flightOrderDetail.ID
                                                   orderby fw.Seq
                                                   select new Contract.FlightWay()
                                                   {
                                                       ID = fw.ID,
                                                       DBDepartDate = fw.DepartDate,
                                                       //DepartDate = fw.DepartDate,
                                                       //出发时间 = fw.DepartDate.ToString("HHmm"),
                                                       DepartLocation = fw.DepartLocation.Trim(),
                                                       Destination = fw.Destination.Trim(),
                                                       FlightNo = fw.FlightNo.Trim(),
                                                       IsDelete = fw.IsDelete,
                                                       Seat = fw.Seat.Trim(),
                                                   }).ToList();
                flightOrderDetail.FlightChangeHis = (from fc in entities.FlightChange
                                                     where fc.FlightOrderDetailID == flightOrderDetail.ID
                                                     select new Contract.FlightChange()
                                                     {
                                                         ID = fc.ID,
                                                         ExchangeRate = fc.ExchangeRate,
                                                         OrderDate = fc.OrderDate,
                                                         OrderUser = new Contract.User()
                                                         {
                                                             ID = fc.OrderUserID
                                                         },
                                                         Price = fc.Price,
                                                         Ref = fc.Ref.Trim(),
                                                         Remark = fc.Remark.Trim(),
                                                         VendorPrice = fc.VendorPrice
                                                     }).ToList();
                flightOrderDetail.Vendor = new MasterBL().GetVendorByID(flightOrderDetail.Vendor.ID);
                flightOrderDetail.VendorCurrency = new BaseBL().GetCurrencyByID(flightOrderDetail.VendorCurrency.ID);
                var vendorSettlement = (from vs in entities.VendorSettlement
                                        where vs.SourceID == flightOrderDetail.ID
                                        select vs).SingleOrDefault();
                if (vendorSettlement != null)
                {
                    flightOrderDetail.PaymentType = vendorSettlement.PayType.Trim();
                    if (vendorSettlement.BankAccountID != null)
                        flightOrderDetail.BankAccount = new MasterBL().GetBankAccountByID(vendorSettlement.BankAccountID.Value);
                }

                foreach (Contract.FlightChange flightChange in flightOrderDetail.FlightChangeHis)
                {
                    flightChange.OrderUser = new BaseBL().GetUserByID(flightChange.OrderUser.ID);
                    flightChange.VendorCurrency = flightOrder.VendorCurrency;
                    flightChange.Currency = flightOrder.Currency;
                    flightChange.Vendor = flightOrderDetail.Vendor;
                }

                flightOrderDetail.FlightReturnHis = (from fr in entities.FlightReturn
                                                     where fr.FlightOrderDetailID == flightOrderDetail.ID
                                                     select new Contract.FlightReturn()
                                                     {
                                                         ID = fr.ID,
                                                         CustomerReturnPrice = fr.CustomerReturnPrice,
                                                         CustomerUsedPrice = fr.CustomerUsedPrice,
                                                         ExchangeRate = fr.ExchangeRate,
                                                         OrderDate = fr.OrderDate,
                                                         OrderUser = new Contract.User()
                                                         {
                                                             ID = fr.OrderUserID
                                                         },
                                                         Ref = fr.Ref.Trim(),
                                                         Remark = fr.Remark.Trim(),
                                                         UsedTax = fr.UsedTax,
                                                         VendorReturnPrice = fr.VendorReturnPrice,
                                                         VendorUsedPrice = fr.VendorUsedPrice
                                                     }).ToList();

                foreach (Contract.FlightReturn flightReturn in flightOrderDetail.FlightReturnHis)
                {
                    flightReturn.OrderUser = new BaseBL().GetUserByID(flightReturn.OrderUser.ID);
                    flightReturn.VendorCurrency = flightOrder.VendorCurrency;
                    flightReturn.Currency = flightOrder.Currency;
                    flightReturn.Vendor = flightOrderDetail.Vendor;
                }

                List<Contract.RFlightChange> flightChangeList
                    = (from fc in entities.FlightChange
                       where fc.FlightOrderDetailID == flightOrderDetail.ID
                       select new Contract.RFlightChange()
                       {
                           ID = fc.ID,
                           OrderDate = fc.OrderDate,
                           PassengerCName = flightOrderDetail.Passenger.CName,
                           PassengerEName = flightOrderDetail.Passenger.EName,
                           Price = fc.Price,
                           VendorPrice = fc.VendorPrice,
                       }).ToList();

                flightOrder.FlightChangeHis.AddRange(flightChangeList);


                List<Contract.RFlightReturn> flightReturnList
                    = (from fr in entities.FlightReturn
                       where fr.FlightOrderDetailID == flightOrderDetail.ID
                       select new Contract.RFlightReturn()
                       {
                           OrderDate = fr.OrderDate,
                           PassengerCName = flightOrderDetail.Passenger.CName,
                           PassengerEName = flightOrderDetail.Passenger.EName,
                           CustomerReturnPrice = fr.CustomerReturnPrice,
                           VendorReturnPrice = fr.VendorReturnPrice,
                       }).ToList();
                flightOrder.FlightReturnHis.AddRange(flightReturnList);
            }

            flightOrder.FlightReturnHis = flightOrder.FlightReturnHis.OrderBy(r => r.OrderDate).ToList();
            flightOrder.FlightChangeHis = flightOrder.FlightChangeHis.OrderBy(r => r.OrderDate).ToList();
            return flightOrder;
        }

        public List<Contract.RTrainTicketOrder> GetAllTrainTicketOrder()
        {
            return (from tto in entities.TrainTicketOrder
                    orderby tto.Ref descending 
                    select new Contract.RTrainTicketOrder()
                    {
                        ID = tto.ID,
                        Ref = tto.Ref.Trim(),
                        OrderDate = tto.OrderDate,
                        TotalPrice = tto.TotalPrice,
                        Status = tto.Status.Trim(),
                        CustomerStatus = tto.CustomerStatus.Trim(),
                        Customer = (from c in entities.Customer
                                    where c.ID == tto.CustomerID
                                    select c.Name.Trim()).FirstOrDefault(),
                    }).ToList();
        }

        public List<Contract.RTrainTicketOrder> GetTrainTicketOrderByRef(string _ref)
        {
            _ref = _ref.Trim();
            if (_ref == string.Empty)
                throw new BusinessException(VerificationText.请输入订单号);

            return (from tto in entities.TrainTicketOrder
                    where tto.Ref.IndexOf(_ref.Trim()) >= 0
                    orderby tto.Ref descending
                    select new Contract.RTrainTicketOrder()
                    {
                        ID = tto.ID,
                        Ref = tto.Ref.Trim(),
                        OrderDate = tto.OrderDate,
                        TotalPrice = tto.TotalPrice,
                        Status = tto.Status.Trim(),
                        CustomerStatus = tto.CustomerStatus.Trim(),
                        Customer = (from c in entities.Customer
                                    where c.ID == tto.CustomerID
                                    select c.Name.Trim()).FirstOrDefault(),
                    }).ToList();
        }

        public List<Contract.RTrainTicketOrder> GetTrainTicketOrderByCustomer(Contract.RCustomer customer)
        {
            if (customer == null)
                throw new BusinessException(VerificationText.请输入客户);


            return (from tto in entities.TrainTicketOrder
                    where tto.CustomerID == customer.ID
                    orderby tto.Ref descending
                    select new Contract.RTrainTicketOrder()
                    {
                        ID = tto.ID,
                        Ref = tto.Ref.Trim(),
                        OrderDate = tto.OrderDate,
                        TotalPrice = tto.TotalPrice,
                        Status = tto.Status.Trim(),
                        CustomerStatus = tto.CustomerStatus.Trim(),
                        Customer = customer.Name,
                    }).ToList();
        }

        public List<Contract.RTrainTicketOrder> GetTrainTicketOrderByOrderDate(DateTime startDate, DateTime endDate)
        {

            return (from tto in entities.TrainTicketOrder
                    where tto.OrderDate >= startDate && tto.OrderDate <= endDate
                    orderby tto.Ref descending
                    select new Contract.RTrainTicketOrder()
                    {
                        ID = tto.ID,
                        Ref = tto.Ref.Trim(),
                        OrderDate = tto.OrderDate,
                        TotalPrice = tto.TotalPrice,
                        Status = tto.Status.Trim(),
                        CustomerStatus = tto.CustomerStatus.Trim(),
                        Customer = (from c in entities.Customer
                                    where c.ID == tto.CustomerID
                                    select c.Name.Trim()).FirstOrDefault(),
                    }).ToList();
        }

        public List<Contract.RTrainTicketOrder> GetTrainTicketOrderByVendor(Contract.RVendor vendor)
        {
            if (vendor == null)
                throw new BusinessException(VerificationText.请输入供应商);


            return (from tto in entities.TrainTicketOrder
                    where tto.VendorID == vendor.ID
                    orderby tto.Ref descending
                    select new Contract.RTrainTicketOrder()
                    {
                        ID = tto.ID,
                        Ref = tto.Ref.Trim(),
                        OrderDate = tto.OrderDate,
                        TotalPrice = tto.TotalPrice,
                        Status = tto.Status.Trim(),
                        CustomerStatus = tto.CustomerStatus.Trim(),
                        Customer = (from c in entities.Customer
                                    where c.ID == tto.CustomerID
                                    select c.Name.Trim()).FirstOrDefault(),
                    }).ToList();
        }

        public Contract.PTrainOrder GetPrintTrainOrder(Guid id)
        {
            Contract.PTrainOrder trainOrder =
                (from tto in entities.TrainTicketOrder
                 where tto.ID == id
                 select new Contract.PTrainOrder()
                 {
                     ID = tto.ID,
                     客户地址 = tto.Address.Trim(),
                     客户名称 = tto.Customer.Name.Trim(),
                     练习电话 = tto.ContactPersonTel.Trim(),
                     结算方式 = tto.CreditTerm.Trim(),
                     DeliveryDate = tto.DeliveryDate,
                     订单号 = tto.Ref.Trim(),
                     备注 = tto.Remark.Trim(),
                     经办人 = tto.User3.Name.Trim(),
                     订单人 = tto.User1.Name.Trim(),
                     总金额 = tto.TotalPrice,
                     联系人 = tto.ContactPerson,
                 }).SingleOrDefault();

            trainOrder.Details =
                (from ttod in entities.TrainTicketOrderDetail 
                 where ttod.TrainTicketOrderID == trainOrder.ID
                 orderby ttod.DepartDate
                 select new Contract.PTrainOrderDetail()
                 {
                     DepartLocation = ttod.DepartLocation.Trim(),
                     Destination = ttod.Destination.Trim(),
                     DeliveryCost = ttod.DeliveryCost,
                     DepartDate = ttod.DepartDate,
                     Qty = ttod.Qty,
                     SeatType = ttod.SeatType.Trim(),
                     TicketPrice = ttod.TicketPrice,
                     TotalPrice = ttod.TotalPrice,
                     TrainNo = ttod.TrainNo.Trim(),
                 }).ToList();

            return trainOrder;
        }

        public Contract.TrainTicketOrder GetTrainTicketOrderByID(Guid id)
        {

            Contract.TrainTicketOrder trainTicketOrder =
                (from tto in entities.TrainTicketOrder
                 where tto.ID == id
                 select new Contract.TrainTicketOrder()
                 {
                     ID = tto.ID,
                     Address = tto.Address.Trim(),
                     ContactPerson = tto.ContactPerson.Trim(),
                     ContactPersonTel = tto.ContactPersonTel.Trim(),
                     CreditTerm = tto.CreditTerm.Trim(),
                     Currency = new Contract.Currency() { ID = tto.CurrencyID },
                     Customer = new Contract.Customer() { ID = tto.CustomerID },
                     DeliveryDate = tto.DeliveryDate,
                     ExchangeRate = tto.ExchangeRate,
                     OrderDate = tto.OrderDate,
                     OrderTicketUser = new Contract.User() { ID = tto.OrderTicketUserID },
                     OrderUser = new Contract.User() { ID = tto.OrderUserID },
                     PrintTotalPrice = tto.PrintTotalPrice,
                     ProvideTicketDate = tto.ProvideTicketDate,
                     ProvideTicketUser = new Contract.User() { ID = tto.ProvideTicketUserID },
                     Ref = tto.Ref.Trim(),
                     Remark = tto.Remark.Trim(),
                     Salesman = new Contract.User() { ID = tto.SalesmanID },
                     Status = tto.Status.Trim(),
                     //TicketTotalQty = tto.TicketTotalQty,
                     //TotalPrice = tto.TotalPrice,
                     //TotalProfit = tto.TotalProfit,
                     Vendor = new Contract.Vendor() { ID = tto.VendorID },
                     VendorCurrency = new Contract.Currency() { ID = tto.VendorCurrencyID },
                     VoidCause = tto.VoidCause.Trim()
                 }).SingleOrDefault();

            if (trainTicketOrder == null)
                throw new BusinessException(BusinessFail.火车票订单不存在);

            //var vendorSettlement = (from vs in entities.VendorSettlement
            //                        where vs.SourceID == trainTicketOrder.ID
            //                        select vs).Single();
            //trainTicketOrder.PaymentType = vendorSettlement.PayType.Trim();
            //if (vendorSettlement.BankAccountID != null)
            //    trainTicketOrder.BankAccount = new MasterBL().GetBankAccountByID(vendorSettlement.BankAccountID.Value);

            trainTicketOrder.Currency = new BaseBL().GetCurrencyByID(trainTicketOrder.Currency.ID);
            trainTicketOrder.Customer = new MasterBL().GetCustomerByID(trainTicketOrder.Customer.ID);
            trainTicketOrder.OrderTicketUser = new BaseBL().GetUserByID(trainTicketOrder.OrderTicketUser.ID);
            trainTicketOrder.OrderUser = new BaseBL().GetUserByID(trainTicketOrder.OrderUser.ID);
            trainTicketOrder.ProvideTicketUser = new BaseBL().GetUserByID(trainTicketOrder.ProvideTicketUser.ID);
            trainTicketOrder.Salesman = new BaseBL().GetUserByID(trainTicketOrder.Salesman.ID);
            trainTicketOrder.Vendor = new MasterBL().GetVendorByID(trainTicketOrder.Vendor.ID);
            trainTicketOrder.VendorCurrency = new BaseBL().GetCurrencyByID(trainTicketOrder.VendorCurrency.ID);

            trainTicketOrder.Details = (from ttod in entities.TrainTicketOrderDetail
                                        where ttod.TrainTicketOrderID == trainTicketOrder.ID
                                        select new Contract.TrainTicketOrderDetail()
                                        {
                                            ID = ttod.ID,
                                            DeliveryCost = ttod.DeliveryCost,
                                            DBDepartDate = ttod.DepartDate,
                                            //DepartDate = ttod.DepartDate,
                                            //出发时间 = ttod.DepartDate.ToString("HHmm"),
                                            DepartLocation = ttod.DepartLocation.Trim(),
                                            Destination = ttod.Destination.Trim(),
                                            IsToHK = ttod.IsToHK,
                                            TicketPrice = ttod.TicketPrice,
                                            //Profit = ttod.Profit,
                                            Qty = ttod.Qty,
                                            SeatType = ttod.SeatType.Trim(),
                                            Status = ttod.Status.Trim(),
                                            //TotalPrice = ttod.TicketPrice,
                                            TrainNo = ttod.TrainNo.Trim(),
                                            VendorPrice = ttod.VendorPrice,
                                        }).ToList();

            foreach (Contract.TrainTicketOrderDetail trainTicketOrderDetail in trainTicketOrder.Details)
            {
                List<Contract.RTrainTicketReturn> list =
                    (from ttr in entities.TrainTicketReturn
                     where ttr.TrainTicketOrderDetailID == trainTicketOrderDetail.ID
                     orderby ttr.OrderDate
                     select new Contract.RTrainTicketReturn()
                     {
                         Destination = trainTicketOrderDetail.Destination,
                         DepartLocation = trainTicketOrderDetail.DepartLocation,
                         OrderDate = ttr.OrderDate,
                         Qty = ttr.Qty
                     }).ToList();

                trainTicketOrder.TrainTicketReturnHis.AddRange(list);

                trainTicketOrderDetail.TrainTicketReturnHis =
                    (from ttr in entities.TrainTicketReturn
                     where ttr.TrainTicketOrderDetailID == trainTicketOrderDetail.ID
                     orderby ttr.OrderDate
                     select new Contract.TrainTicketReturn()
                     {
                         ID = ttr.ID,
                         //Currency = trainTicketOrder.Currency,
                         //VendorCurrency = trainTicketOrder.VendorCurrency,
                         ExchangeRate = trainTicketOrder.ExchangeRate,
                         OrderDate = ttr.OrderDate,
                         OrderUser = new Contract.User()
                         {
                             ID = ttr.OrderUserID
                         },
                         Qty = ttr.Qty,
                         Ref = ttr.Ref,
                         ServiceCharge = ttr.ServiceCharge,
                         VendorServiceCharge = ttr.VendorServiceCharge
                     }).ToList();

                foreach (Contract.TrainTicketReturn trainTicketReturn in trainTicketOrderDetail.TrainTicketReturnHis)
                {
                    trainTicketReturn.OrderUser = new BaseBL().GetUserByID(trainTicketReturn.OrderUser.ID);
                    trainTicketReturn.Currency = trainTicketOrder.Currency;
                    trainTicketReturn.VendorCurrency = trainTicketOrder.VendorCurrency;
                }
            }

            trainTicketOrder.TrainTicketReturnHis = trainTicketOrder.TrainTicketReturnHis.OrderBy(t => t.OrderDate).ToList();

            return trainTicketOrder;
        }

        public List<Contract.RHotelBooking> GetAllHotelBooking()
        {
            return (from hb in entities.HotelBooking
                    orderby hb.Ref descending 
                    select new Contract.RHotelBooking()
                    {
                        ID = hb.ID,
                        Ref = hb.Ref.Trim(),
                        OrderDate = hb.OrderDate,
                        TotalPrice = hb.TotalPrice,
                        Status = hb.Status.Trim(),
                        CustomerStatus = hb.CustomerStatus.Trim(),
                        Customer = (from c in entities.Customer
                                    where c.ID == hb.CustomerID
                                    select c.Name.Trim()).FirstOrDefault(),
                    }).ToList();
        }

        public List<Contract.RHotelBooking> GetHotelBookingByRef(string _ref)
        {
            _ref = _ref.Trim();
            if (_ref == string.Empty)
                throw new BusinessException(VerificationText.请输入订单号);


            return (from hb in entities.HotelBooking
                    where hb.Ref.IndexOf(_ref.Trim()) >= 0
                    orderby hb.Ref descending
                    select new Contract.RHotelBooking()
                    {
                        ID = hb.ID,
                        Ref = hb.Ref.Trim(),
                        OrderDate = hb.OrderDate,
                        TotalPrice = hb.TotalPrice,
                        Status = hb.Status.Trim(),
                        CustomerStatus = hb.CustomerStatus.Trim(),
                        Customer = (from c in entities.Customer
                                    where c.ID == hb.CustomerID
                                    select c.Name.Trim()).FirstOrDefault(),
                    }).ToList();
        }

        public List<Contract.RHotelBooking> GetHotelBookingByGuestName(string guestName)
        {
            guestName = guestName.Trim();
            if (guestName == string.Empty)
                throw new BusinessException(VerificationText.请输入入住人姓名);


            return (from hb in entities.HotelBooking
                    where (from hbd in entities.HotelBookingDetail
                           where hbd.HotelBookingID == hb.ID &&
                                 hbd.GuestName.IndexOf(guestName.Trim()) >= 0
                           group hbd by hbd.HotelBookingID into g
                           select g.Key).Any(fodid => hb.ID == fodid)
                    orderby hb.Ref descending
                    select new Contract.RHotelBooking()
                    {
                        ID = hb.ID,
                        Ref = hb.Ref.Trim(),
                        OrderDate = hb.OrderDate,
                        TotalPrice = hb.TotalPrice,
                        Status = hb.Status.Trim(),
                        CustomerStatus = hb.CustomerStatus.Trim(),
                        Customer = (from c in entities.Customer
                                    where c.ID == hb.CustomerID
                                    select c.Name.Trim()).FirstOrDefault(),
                    }).ToList();
        }

        public List<Contract.RHotelBooking> GetHotelBookingByCustomer(Contract.RCustomer customer)
        {
            if (customer == null)
                throw new BusinessException(VerificationText.请输入客户);


            return (from hb in entities.HotelBooking
                    where hb.CustomerID == customer.ID
                    orderby hb.Ref descending
                    select new Contract.RHotelBooking()
                    {
                        ID = hb.ID,
                        Ref = hb.Ref.Trim(),
                        OrderDate = hb.OrderDate,
                        TotalPrice = hb.TotalPrice,
                        Status = hb.Status.Trim(),
                        CustomerStatus = hb.CustomerStatus.Trim(),
                        Customer = customer.Name
                    }).ToList();
        }

        public List<Contract.RHotelBooking> GetHotelBookingByOrderDate(DateTime startDate, DateTime endDate)
        {

            return (from hb in entities.HotelBooking
                    where hb.OrderDate >= startDate && hb.OrderDate <= endDate
                    orderby hb.Ref descending
                    select new Contract.RHotelBooking()
                    {
                        ID = hb.ID,
                        Ref = hb.Ref.Trim(),
                        OrderDate = hb.OrderDate,
                        TotalPrice = hb.TotalPrice,
                        Status = hb.Status.Trim(),
                        CustomerStatus = hb.CustomerStatus.Trim(),
                        Customer = (from c in entities.Customer
                                    where c.ID == hb.CustomerID
                                    select c.Name.Trim()).FirstOrDefault(),
                    }).ToList();
        }

        public List<Contract.RHotelBooking> GetHotelBookingByVendor(Contract.RVendor vendor)
        {
            if (vendor == null)
                throw new BusinessException(VerificationText.请输入供应商);


            return (from hb in entities.HotelBooking
                    where hb.VendorID == vendor.ID
                    orderby hb.Ref descending
                    select new Contract.RHotelBooking()
                    {
                        ID = hb.ID,
                        Ref = hb.Ref.Trim(),
                        OrderDate = hb.OrderDate,
                        TotalPrice = hb.TotalPrice,
                        Status = hb.Status.Trim(),
                        CustomerStatus = hb.CustomerStatus.Trim(),
                        Customer = (from c in entities.Customer
                                    where c.ID == hb.CustomerID
                                    select c.Name.Trim()).FirstOrDefault(),
                    }).ToList();
        }

        public Contract.HotelBooking GetHotelBookingByID(Guid id)
        {

            Contract.HotelBooking hotelBooking =
                (from hb in entities.HotelBooking
                 where hb.ID == id
                 select new Contract.HotelBooking()
                 {
                     ID = hb.ID,
                     Address = hb.Address.Trim(),
                     ContactPerson = hb.ContactPerson.Trim(),
                     ContactPersonTel = hb.ContactPersonTel.Trim(),
                     Currency = new Contract.Currency() { ID = hb.CurrencyID },
                     VendorCurrency = new Contract.Currency() { ID = hb.VendorCurrencyID },
                     Customer = new Contract.Customer() { ID = hb.CustomerID },
                     //Intergral = hb.Intergral,
                     OrderDate = hb.OrderDate,
                     OrderHotelUser = new Contract.User() { ID = hb.OrderHotelUserID },
                     OrderUser = new Contract.User() { ID = hb.OrderUserID },
                     ProvideTicketUser = new Contract.User() { ID = hb.ProvideTicketUserID },
                     ProvideTicketDate = hb.ProvideTicketDate,
                     DeliveryDate = hb.DeliveryDate,
                     CustomerPaymentType = hb.PaymentType.Trim(),
                     PrintTotalPrice = hb.PrintTotalPrice,
                     ExchangeRate = hb.ExchangeRate,
                     Ref = hb.Ref.Trim(),
                     Remark = hb.Remark.Trim(),
                     CustomerStatus = hb.CustomerStatus.Trim(),
                     //RoomTotalQty = hb.RoomTotalQty,
                     Salesman = new Contract.User() { ID = hb.SalesmanID },
                     Status = hb.Status.Trim(),
                     //TotalPrice = hb.TotalPrice,
                     //TotalVendorPrice = hb.TotalVendorPrice,
                     //TotalProfit = hb.TotalPrice,
                     Vendor = new Contract.Vendor() { ID = hb.VendorID },
                     VoidCause = hb.VoidCause.Trim()
                 }).SingleOrDefault();

            if (hotelBooking == null)
                throw new BusinessException(BusinessFail.酒店订单不存在);

            //var vendorSettlement = (from vs in entities.VendorSettlement
            //                        where vs.SourceID == hotelBooking.ID
            //                        select vs).Single();
            //hotelBooking.PaymentType = vendorSettlement.PayType.Trim();
            //if (vendorSettlement.BankAccountID != null)
            //    hotelBooking.BankAccount = new MasterBL().GetBankAccountByID(vendorSettlement.BankAccountID.Value);

            hotelBooking.Currency = new BaseBL().GetCurrencyByID(hotelBooking.Currency.ID);
            hotelBooking.VendorCurrency = new BaseBL().GetCurrencyByID(hotelBooking.VendorCurrency.ID);
            hotelBooking.Customer = new MasterBL().GetCustomerByID(hotelBooking.Customer.ID);
            hotelBooking.OrderHotelUser = new BaseBL().GetUserByID(hotelBooking.OrderHotelUser.ID);
            hotelBooking.OrderUser = new BaseBL().GetUserByID(hotelBooking.OrderUser.ID);
            hotelBooking.ProvideTicketUser = new BaseBL().GetUserByID(hotelBooking.ProvideTicketUser.ID);
            hotelBooking.Salesman = new BaseBL().GetUserByID(hotelBooking.Salesman.ID);
            hotelBooking.Vendor = new MasterBL().GetVendorByID(hotelBooking.Vendor.ID);

            hotelBooking.Details =
                (from hbd in entities.HotelBookingDetail
                 where hbd.HotelBookingID == hotelBooking.ID
                 select new Contract.HotelBookingDetail()
                 {
                     ID = hbd.ID,
                     CheckInDate = hbd.CheckInDate,
                     CheckOutDate = hbd.CheckOutDate,
                     //Days = hbd.Days,
                     GuestName = hbd.GuestName.Trim(),
                     HotelConfirmationRef = hbd.HotelConfirmationRef.Trim(),
                     HotelLeave = hbd.HotelLevel.Trim(),
                     HotelName = hbd.HotelName.Trim(),
                     HotelOrderRef = hbd.HotelOrderRef.Trim(),
                     HotelTax = hbd.HotelTax,
                     Intergral = hbd.Intergral,
                     Price = hbd.Price,
                     //Profit = hbd.Profit,
                     Rooms = hbd.Rooms,
                     RoomType = hbd.RoomType.Trim(),
                     //TotalPrice = hbd.TotalPrice,
                     VendorDiscount = hbd.VendorDiscount,
                     VendorPrice = hbd.VendorPrice,
                 }).ToList();

            return hotelBooking;
        }

        public List<Contract.ROtherBusiness> GetAllOtherBusiness()
        {
            return (from ob in entities.OtherBusiness
                    orderby ob.Ref descending
                    select new Contract.ROtherBusiness()
                    {
                        ID = ob.ID,
                        Ref = ob.Ref.Trim(),
                        OrderDate = ob.OrderDate,
                        TotalPrice = ob.TotalPrice,
                        ProjectDate = ob.ProjectDate,
                        Status = ob.Status.Trim(),
                        CustomerStatus = ob.CustomerStatus.Trim(),
                        Project = (from p in entities.Project
                                   where p.ID == ob.ProjectID
                                   select p.Name.Trim()).FirstOrDefault(),
                        SubProject = (from sp in entities.SubProject
                                      where sp.ID == ob.SubProjectID
                                      select sp.Name.Trim()).FirstOrDefault(),
                        Customer = (from c in entities.Customer
                                    where c.ID == ob.CustomerID
                                    select c.Name.Trim()).FirstOrDefault(),
                    }).ToList();
        }

        public List<Contract.ROtherBusiness> GetOtherBusinessByRef(string _ref)
        {
            _ref = _ref.Trim();
            if (_ref == string.Empty)
                throw new BusinessException(VerificationText.请输入订单号);


            return (from ob in entities.OtherBusiness
                    where ob.Ref.IndexOf(_ref.Trim()) >= 0
                    orderby ob.Ref descending
                    select new Contract.ROtherBusiness()
                    {
                        ID = ob.ID,
                        Ref = ob.Ref.Trim(),
                        OrderDate = ob.OrderDate,
                        TotalPrice = ob.TotalPrice,
                        ProjectDate = ob.ProjectDate,
                        Status = ob.Status.Trim(),
                        CustomerStatus = ob.CustomerStatus.Trim(),
                        Project = (from p in entities.Project
                                   where p.ID == ob.ProjectID
                                   select p.Name.Trim()).FirstOrDefault(),
                        SubProject = (from sp in entities.SubProject
                                      where sp.ID == ob.SubProjectID
                                      select sp.Name.Trim()).FirstOrDefault(),
                        Customer = (from c in entities.Customer
                                    where c.ID == ob.CustomerID
                                    select c.Name.Trim()).FirstOrDefault(),
                    }).ToList();
        }

        public List<Contract.ROtherBusiness> GetOtherBusinessByCustomer(Contract.RCustomer customer)
        {
            if (customer == null)
                throw new BusinessException(VerificationText.请输入客户);


            return (from ob in entities.OtherBusiness
                    where ob.CustomerID == customer.ID
                    orderby ob.Ref descending
                    select new Contract.ROtherBusiness()
                    {
                        ID = ob.ID,
                        Ref = ob.Ref.Trim(),
                        OrderDate = ob.OrderDate,
                        TotalPrice = ob.TotalPrice,
                        ProjectDate = ob.ProjectDate,
                        Status = ob.Status.Trim(),
                        CustomerStatus = ob.CustomerStatus.Trim(),
                        Project = (from p in entities.Project
                                   where p.ID == ob.ProjectID
                                   select p.Name.Trim()).FirstOrDefault(),
                        SubProject = (from sp in entities.SubProject
                                      where sp.ID == ob.SubProjectID
                                      select sp.Name.Trim()).FirstOrDefault(),
                        Customer = customer.Name
                    }).ToList();
        }

        public List<Contract.ROtherBusiness> GetOtherBusinessByOrderDate(DateTime startDate, DateTime endDate)
        {

            return (from ob in entities.OtherBusiness
                    where ob.OrderDate >= startDate && ob.OrderDate <= endDate
                    orderby ob.Ref descending
                    select new Contract.ROtherBusiness()
                    {
                        ID = ob.ID,
                        Ref = ob.Ref.Trim(),
                        OrderDate = ob.OrderDate,
                        TotalPrice = ob.TotalPrice,
                        ProjectDate = ob.ProjectDate,
                        Status = ob.Status.Trim(),
                        CustomerStatus = ob.CustomerStatus.Trim(),
                        Project = (from p in entities.Project
                                   where p.ID == ob.ProjectID
                                   select p.Name.Trim()).FirstOrDefault(),
                        SubProject = (from sp in entities.SubProject
                                      where sp.ID == ob.SubProjectID
                                      select sp.Name.Trim()).FirstOrDefault(),
                        Customer = (from c in entities.Customer
                                    where c.ID == ob.CustomerID
                                    select c.Name.Trim()).FirstOrDefault(),
                    }).ToList();
        }

        public List<Contract.ROtherBusiness> GetOtherBusinessByVendor(Contract.RVendor vendor)
        {
            if (vendor == null)
                throw new BusinessException(VerificationText.请输入供应商);


            return (from ob in entities.OtherBusiness
                    where ob.VendorID == vendor.ID
                    orderby ob.Ref descending
                    select new Contract.ROtherBusiness()
                    {
                        ID = ob.ID,
                        Ref = ob.Ref.Trim(),
                        OrderDate = ob.OrderDate,
                        TotalPrice = ob.TotalPrice,
                        ProjectDate = ob.ProjectDate,
                        Status = ob.Status.Trim(),
                        CustomerStatus = ob.CustomerStatus.Trim(),
                        Project = (from p in entities.Project
                                   where p.ID == ob.ProjectID
                                   select p.Name.Trim()).FirstOrDefault(),
                        SubProject = (from sp in entities.SubProject
                                      where sp.ID == ob.SubProjectID
                                      select sp.Name.Trim()).FirstOrDefault(),
                        Customer = (from c in entities.Customer
                                    where c.ID == ob.CustomerID
                                    select c.Name.Trim()).FirstOrDefault(),
                    }).ToList();
        }

        public List<Contract.ROtherBusiness> GetOtherBusinessByProject(Contract.Project project, Contract.SubProject subProject, string content)
        {
            content = content.Trim();
            if (project == null)
                throw new BusinessException(VerificationText.请输入项目);


            if (subProject == null)
            {
                return (from ob in entities.OtherBusiness
                        where ob.ProjectID == project.ID &&
                              ob.Contenct.IndexOf(content.Trim()) >= 0
                        select new Contract.ROtherBusiness()
                        {
                            ID = ob.ID,
                            Ref = ob.Ref.Trim(),
                            OrderDate = ob.OrderDate,
                            TotalPrice = ob.TotalPrice,
                            ProjectDate = ob.ProjectDate,
                            Status = ob.Status.Trim(),
                            CustomerStatus = ob.CustomerStatus.Trim(),
                            Project = (from p in entities.Project
                                       where p.ID == ob.ProjectID
                                       select p.Name.Trim()).FirstOrDefault(),
                            SubProject = (from sp in entities.SubProject
                                          where sp.ID == ob.SubProjectID
                                          select sp.Name.Trim()).FirstOrDefault(),
                            Customer = (from c in entities.Customer
                                        where c.ID == ob.CustomerID
                                        select c.Name.Trim()).FirstOrDefault(),
                        }).ToList();
            }
            else
            {
                return (from ob in entities.OtherBusiness
                        where ob.ProjectID == project.ID &&
                              ob.SubProjectID == subProject.ID &&
                              ob.Contenct.IndexOf(content.Trim()) >= 0
                        select new Contract.ROtherBusiness()
                        {
                            ID = ob.ID,
                            Ref = ob.Ref.Trim(),
                            OrderDate = ob.OrderDate,
                            TotalPrice = ob.Price,
                            ProjectDate = ob.ProjectDate,
                            Status = ob.Status.Trim(),
                            CustomerStatus = ob.CustomerStatus.Trim(),
                            Project = (from p in entities.Project
                                       where p.ID == ob.ProjectID
                                       select p.Name.Trim()).FirstOrDefault(),
                            SubProject = (from sp in entities.SubProject
                                          where sp.ID == ob.SubProjectID
                                          select sp.Name.Trim()).FirstOrDefault(),
                            Customer = (from c in entities.Customer
                                        where c.ID == ob.CustomerID
                                        select c.Name.Trim()).FirstOrDefault(),
                        }).ToList();
            }
        }

        public Contract.OtherBusiness GetOtherBusinessByID(Guid id)
        {

            Contract.OtherBusiness otherBusiness =
                (from ob in entities.OtherBusiness
                 where ob.ID == id
                 select new Contract.OtherBusiness()
                 {
                     ID = ob.ID,
                     Address = ob.Address.Trim(),
                     BasePrice = ob.BasePrice,
                     ContactPerson = ob.ContactPerson.Trim(),
                     ContactPersonTel = ob.ContactPersonTel.Trim(),
                     Content = ob.Contenct.Trim(),
                     CreditTerm = ob.CreditTerm.Trim(),
                     Intergral = ob.Intergral,
                     OrderDate = ob.OrderDate,
                     Price = ob.Price,
                     PrintTotalPrice = ob.PrintTotalPrice,
                     ProjectDate = ob.ProjectDate,
                     Qty = ob.Qty,
                     Ref = ob.Ref.Trim(),
                     Remark = ob.Remark.Trim(),
                     Status = ob.Status.Trim(),
                     VendorDiscount = ob.VendorDiscount,
                     VoidCause = ob.VoidCause,
                     CustomerName = ob.CustomerName.Trim(),
                     Salesman = new Contract.User() { ID = ob.SalesmanID },
                     ProvideTicketUser = new Contract.User { ID = ob.ProvideTicketUserID },
                     Currency = new Contract.Currency() { ID = ob.CurrencyID },
                     Customer = new Contract.Customer() { ID = ob.CustomerID },
                     OrderUser = new Contract.User() { ID = ob.OrderUserID },
                     Project = new Contract.Project() { ID = ob.ProjectID },
                     Vendor = new Contract.Vendor() { ID = ob.VendorID },
                     SubProjectID = ob.SubProjectID,
                     DeliveryDate = ob.DeliveryDate
                 }).Single();

            if (otherBusiness == null)
                throw new BusinessException(BusinessFail.其他项目订单不存在);

            //var vendorSettlement = (from vs in entities.VendorSettlement
            //                        where vs.SourceID == otherBusiness.ID
            //                        select vs).Single();
            //otherBusiness.PaymentType = vendorSettlement.PayType.Trim();
            //if (vendorSettlement.BankAccountID != null)
            //    otherBusiness.BankAccount = new MasterBL().GetBankAccountByID(vendorSettlement.BankAccountID.Value);

            otherBusiness.Salesman = new BaseBL().GetUserByID(otherBusiness.Salesman.ID);
            otherBusiness.ProvideTicketUser = new BaseBL().GetUserByID(otherBusiness.ProvideTicketUser.ID);
            otherBusiness.Currency = new BaseBL().GetCurrencyByID(otherBusiness.Currency.ID);
            otherBusiness.Customer = new MasterBL().GetCustomerByID(otherBusiness.Customer.ID);
            otherBusiness.OrderUser = new BaseBL().GetUserByID(otherBusiness.OrderUser.ID);
            otherBusiness.Project = new MasterBL().GetProjectByID(otherBusiness.Project.ID);
            if (otherBusiness.SubProjectID != null)
                otherBusiness.SubProject = otherBusiness.Project.SubProjectList.Find(sp => sp.ID == otherBusiness.SubProjectID);
            otherBusiness.Vendor = new MasterBL().GetVendorByID(otherBusiness.Vendor.ID);

            return otherBusiness;
        }

        #endregion

        #endregion

        public List<Contract.IntergralExchange> GetIntergralExchangeByMonth(DateTime month)
        {
            DateTime startDateTime = new DateTime(month.Year, month.Month, 1);
            DateTime endDateTime = startDateTime.AddMonths(1).AddMilliseconds(-5);

            return (from ie in entities.IntergralExchange
                    where ie.Date >= startDateTime &&
                          ie.Date <= endDateTime
                    orderby ie.Date
                    select new Contract.IntergralExchange()
                    {
                        ID = ie.ID,
                        Amount = ie.Amount,
                        CustomerText = (from c in entities.Customer
                                        where c.ID == ie.CustomerID
                                        select c.Name.Trim()).FirstOrDefault(),
                        Date = ie.Date,
                        Item = ie.Item.Trim(),
                        Qty = ie.Qty,
                        Receiptor = ie.Receiptor,
                        Remark = ie.Remark.Trim()
                    }).ToList();
        }

        private DateTime convertToDateTime(DateTime dateTime, string time)
        {
            if (time.Length != 4)
                throw new BusinessException(VerificationText.出发时间格式不正确);

            string sHour = time.Substring(0, 2);
            string sMinute = time.Substring(2, 2);
            try
            {
                int hour = Convert.ToInt32(sHour);
                int minute = Convert.ToInt32(sMinute);

                return new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, hour, minute, 0);
            }
            catch (Exception)
            {
                throw new BusinessException(VerificationText.出发时间格式不正确);
            }

        }
    }
}
